Skip to content
Advertisement

JPA @PreUpdate @Persist seems not working as expected

I have a problem for filling auditing fields by using @PreUpdate and @PrePersist. For instance When I’d like to update a client entity, the field updatedBy and updatedAt are still null; despite when I debug, the code of preUpdate() which is annotated with @PreUpdate is executed.

Below the code of AuditingField which is responsible for creating/updating the auditing fields in each JPA entity:

JavaScript

The client entity that contains embedded auditing fields:

JavaScript

The method that updates the client entity

JavaScript

And finally the persistence context configuration:

JavaScript

I’m thankful for any help.

Advertisement

Answer

You are using updatable=false on those columns:

JavaScript

This means that JPA doesn’t use this field to update the column. From the JPA spec for updatable:

Whether the column is included in SQL UPDATE statements generated by the persistence provider.

This make sense for the createdBy or createdAt columns, that are set on @PrePersist and persisted with the first INSERT, and you don’t want them to be modified afterwards. But columns updated in @PreUpdate (or @PreRemove) wont be updated with the UPDATE statement if updatable is set to false

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement