Skip to content

Consider allowing attributes annotated with CreatedDate to determine "isNew" status for R2DBC Repositories #1587

Closed
@efenderbosch-atg

Description

@efenderbosch-atg

Affects: r2dbc 6, data-commons 3.1

Currently r2dbc "is new state detection" supports:

  • null/0 @Id properties
  • null/0 @Version properties
  • implementing Persistable::isNew

Another possibility is null @CreatedDate properties. I personally don't like nullable id properties and a version property doesn't make sense for my current project since there's only 1 user mutating an entity at a time, but the service does mutate entities, which was causing OptimisticLockingFailureException to be thrown when an old version comes through the API.

My solution was to implement Persistable, but it might be nice to support null @CreatedDate properties natively as an alternative"is new state detection".

My entities look like this:

@Immutable
interface AuditedPersistable<T> : Persistable<T> {
    @get:CreatedDate
    val createdAt: Instant?

    @get:LastModifiedDate
    val updatedAt: Instant?

    @JsonIgnore
    override fun isNew() = createdAt == null
}

@Table
data class Foo(
    private val id: UUIDv7,
    val bar: String,
    override val createdAt: Instant?,
    override val updatedAt: Instant?,
) : AuditedPersistable<UUIDv7> {
    @Id
    override fun getId() = id
}

I did try the custom EntityInformation route, but that proved to be too difficult when compared to the above solution.

Or maybe just add documentation with something like this as an example.

Metadata

Metadata

Assignees

Labels

status: declinedA suggestion or change that we don't feel we should currently apply

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions