-
Notifications
You must be signed in to change notification settings - Fork 349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using audit annotation in an embedded Java record fails with Cannot set property
#1694
Comments
Is this specific to the embeded entity being a record? |
If i remember well, I reproduced the issue with classes instead of record |
The problem isn't specific to records, I've encountered it with regular java class - I've attached example project. This is embedded class: @Value
@Builder
@Immutable
public class Audit {
private static final Audit EMPTY = builder().build();
@CreatedDate
Instant createdAt;
@CreatedBy
String createdBy;
@LastModifiedDate
Instant updatedAt;
@LastModifiedBy
String updatedBy;
public static Audit empty() {
return EMPTY;
}
} and entity class: @Value
@Builder(toBuilder = true)
@Immutable
@Table
public class TestEntity {
@Id
String id;
@Version
int version;
@NonNull
String name;
@NonNull
@Embedded.Empty
@Builder.Default
Audit audit = Audit.empty();
} When spring boot version 3.1.7 is used (you can easilty change it in Full stacktrace:
|
If the embedded entity containing the audit annotation is a regular class with setter then it's work. FYI, on version 2.4.11, the record was supported. @schauder When I debug it's failing on
because it's searching an |
Ran into the same issue as reported by @jipipi, but instead of getting an error, no error occurs and no auditing information if persisted to the database. Works: public record MyEntity(
@Id Integer id,
@CreatedBy AggregateReference<User, Integer> createdBy,
@CreatedDate Instant creationDate,
@LastModifiedBy AggregateReference<User, Integer> lastModifiedBy,
@LastModifiedDate Instant lastModifiedDate) {} Fails: public record MyEntity(
@Id Integer id,
@Embedded.Empty AuditMetadata auditMetadata) {}
public record AuditMetadata(
@CreatedBy AggregateReference<User, Integer> createdBy,
@CreatedDate Instant creationDate,
@LastModifiedBy AggregateReference<User, Integer> lastModifiedBy,
@LastModifiedDate Instant lastModifiedDate){} My use case is reusing the same embedded AuditMetadata record in multiple entities. |
This is the expected behaviour when the embedded entity is |
Cannot set property
The reason for auditing to not work on embedded fields is that EmbeddedRelationalPersistentProperty and BasicRelationalPersisntentProperty were not considered equal even when they represent the same field. Note: the fix is somewhat hackish since it breaks the equals contract for EmbeddedRelationalPersistentProperty. Closes #1694 See spring-projects/spring-data-mongodb@1545e18
The reason for auditing to not work on embedded fields is that EmbeddedRelationalPersistentProperty and BasicRelationalPersisntentProperty were not considered equal even when they represent the same field. Note: the fix is somewhat hackish since it breaks the equals contract for EmbeddedRelationalPersistentProperty. Closes #1694 See spring-projects/spring-data-mongodb@1545e18
The reason for auditing to not work on embedded fields is that EmbeddedRelationalPersistentProperty and BasicRelationalPersisntentProperty were not considered equal even when they represent the same field. Note: the fix is somewhat hackish since it breaks the equals contract for EmbeddedRelationalPersistentProperty. Closes #1694 See spring-projects/spring-data-mongodb@1545e18
If you declare audit annotation like
@CreatedBy
in an embedded java record then an exception is thrown :IllegalState Cannot set property createdUser because no setter, no wither and it's not part of the persistence constructor public springdata.jdbc.bug.UserAudit(java.lang.String,java.time.Instant,java.lang.String,java.time.LocalDateTime)
This issue also happens for other audit annotation
@LastModifiedBy,
@LastModifiedDate
and@CreatedDate
If the annotation are not in an embedded object it's work.
Exemple of code to reproduce issue
Here a project to reproduce the bug (the test failed) : https://github.com/jipipi/spring-data-jdbc-bug1
spring-data-jdbc : 3.2.0
java 17
The text was updated successfully, but these errors were encountered: