Closed
Description
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
public record Car (
@Id Long id,
String name,
@Embedded.Empty()
UserAudit userAudit
){}
public record UserAudit(@LastModifiedBy String modifiedUser,
@LastModifiedDate Instant modifiedDate,
@CreatedBy String createdUser,
@CreatedDate LocalDateTime createdDate) {}
CREATE TABLE IF NOT EXISTS Car (
id INTEGER IDENTITY PRIMARY KEY,
name VARCHAR(100),
-- UserAudit attributes
modified_user varchar(255),
modified_date timestamp,
created_user varchar(255),
created_date timestamp
);
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
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
[-]Using audit annotation in an embedded java record not wort[/-][+]Using audit annotation in an embedded java record not work[/+]schauder commentedon Jan 3, 2024
Is this specific to the embeded entity being a record?
jipipi commentedon Jan 4, 2024
If i remember well, I reproduced the issue with classes instead of record
grzegorzt commentedon Jan 8, 2024
The problem isn't specific to records, I've encountered it with regular java class - I've attached example project.
This is embedded class:
and entity class:
When spring boot version 3.1.7 is used (you can easilty change it in
build.gradle
), everything works. In 3.2.1 the exception is thrown:java.lang.IllegalStateException: Cannot set property createdBy because no setter, no wither and it's not part of the persistence constructor ....
Full stacktrace:
bug.tar.gz
[-]Using audit annotation in an embedded java record not work[/-][+]Using audit annotation in an embedded java object not work[/+]jipipi commentedon Feb 8, 2024
If the embedded entity containing the audit annotation is a regular class with setter then it's work.
I updated the test case :
bug1.zip
FYI, on version 2.4.11, the record was supported.
@schauder When I debug it's failing on
InstantiationAwarePropertyAccessor
:because it's searching an

EmbeddedRelationalPersistencePoperty
andBasicJdbcPersistentProperty
is provided:dennishendriksen commentedon Mar 1, 2024
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:
Fails:
My use case is reusing the same embedded AuditMetadata record in multiple entities.
DreamStar92 commentedon Jun 26, 2024
@schauder
The

owner
(RelationalPersistentEntity
) returned byEmbeddedRelationalPersistentProperty
is theowner
of its delegate. The creator of the owner gets the creation value from its own properties. The property ofRelationalPersistentEntity
isBasicRelationalPersistentProperty
, which meansisCreatorParameter
->doGetIsCreatorParameter
->maps->property.equals(referencedProperty)
, property isEmbeddedRelationalPersistentProperty
andreferencedProperty
isBasicRelationalPersistentProperty
, causing false to be returned.schauder commentedon Jun 26, 2024
This is the expected behaviour when the embedded entity is
null
.We are not instantiating such entities on the fly.
[-]Using audit annotation in an embedded java object not work[/-][+]Using audit annotation in an embedded Java record fails with `Cannot set property`[/+]@Unwrapped
auditing objects fails withCannot set property
spring-projects/spring-data-mongodb#4732Fix Auditing for embedded fields.
Fix Auditing for embedded fields.
Fix Auditing for embedded fields.