Closed imuhdork closed 4 months ago
Spring Data JPA uses a @PrePersist
/@PreUpdate
lifecycle hook via AuditingEntityListener
. While I wasn't immediately able to find any JPA rules that would prevent updating the identifier in a lifecycle method, I can well imagine why updating an identifier isn't a great idea.
Spring Data JPA has upgraded with 3.2.x to Hibernate 6.4 whereas Spring Data 3.1.x used Hibernate 6.2.
Embeddables aren't necessarily identifiers and the check that raises the exception relies on the implementation of equals/hashCode. The exception comes from within Hibernate so I suggest that you create a reproducer using purely Hibernate API for @PrePersist
updating an identifier (id-class) and report it against Hibernate.
Putting this here in case I get nowhere with hibernate. I was able to reproduce the issue removing auditing completely and just using @PrePersist
. The @PrePersist
method and tests work fine in 6.2.21.Final and the @IdClass
test case fails in 6.2.22.Final.
It appears that between 6.2.21.Final and 6.2.22.Final the composite key @Id
values set in a @PrePersist
method on an @Entity
get overwritten by hibernate. In 6.2.21.Final the tests pass, in 6.2.21.Final the @IdClass
test fails because the @Id
values are null even though the are set in the @PrePersist method. It is only when we get to 6.3.1.Final when the HibernateException
is thrown.
Going even further, not only are the @Id
properties overwritten but it appears that @PrePersist
gets called twice in 6.2.22.Final and beyond in the @IdClass
test case and only once in the @Embeddable
test case.
Created hibernate defect and test case template compliant repo.
Using
@CreatedDate
and@CreatedBy
along withAuditingEntityListener
and anAuditorAware
Bean fails with appropriately annotated entities, composite keys, and repository interfaces when you using@IdClass
pattern but succeeds when using the@Embeddable
/@EmbeddedId
pattern. Both patterns was working in spring data 3.1.x but stopped working in 3.2.x.I attached a 3.1.6 version and a 3.2.5 version of a small maven style project recreating the issue. In the attached there are two key tests. Both tests pass in the 3.1.6 version of the attached project. The test using an
@Embeddable
composite key with an@EmbeddedId
property of an entity passes in the 3.2.5 version without exception and the@CreatedBy
and@CreatedDate
are correctly persisted in the in-memory db. The test using an@IdClass
annotated entity with a composite key class fails in the 3.2.5 version with the following exception:I am not 100% sure where exactly the issue is occurring but I think it is in spring data handling of audited fields as I would expect both of these styles to behave similarly with respect to auditing? Additionally, the fact that both tests pass in 3.1.6 one test fails in 3.2.5 confirms that auditing behavior worked at one point regardless of the style used to configure the composite key.