I've hit a blocker adding a fix to an existing project.the main problem is that i'll like to use @Prepersist
and @PreUpdate
in the POJO
to take care of LastModified
field (insert and update) using hibernate implementation of JPA with session.
Reason ?: That change is required because there is a need to use liquibase 1.9.5
and i know (since i've faced this before) that liquibase translate timestamp fied to datetime with default current_timestamp, and that is too bad for mysql database.
So i needed a way to have this set in code rather than in database so i could safely change timestamp field to datetime.then liquibase is happy, i'm happy.
Now it seems that those interpreters are not been executed, with little search i found out that it's suitable using entityManager
.That is currently out of question.So i'll like to know if is there a SIMPLE way around my problem, meaning having @Prepersist
or @PreUpdate
or even other workaround to set the lastModified
field still using session
The @PrePersist and @PreUpdate annotations are JPA annotations introduced in JPA 1.0. Both annotations are used to configure callbacks that are triggered on specific entity lifecycle events. The @PrePersist annotation is used to configure a callback for pre-persist(pre-insert) events of the entity.
prePersist - The prePersist event occurs for a given entity before the respective EntityManager persist operation for that entity is executed. preUpdate - The preUpdate event occurs before the database update operations to entity data. It is not called for a DQL UPDATE statement.
before persist is called for a new entity – @PrePersist. after persist is called for a new entity – @PostPersist. before an entity is removed – @PreRemove. after an entity has been deleted – @PostRemove. before the update operation – @PreUpdate.
Annotation Type PreUpdateSpecifies a callback method for the corresponding lifecycle event. This annotation may be applied to methods of an entity class, a mapped superclass, or a callback listener class. Since: Java Persistence 1.0.
Now it seems that those interpreters are not been executed, with little search i found out that it's suitable using entityManager.
Yes, the JPA callbacks won't work if you're using the Session
API.
So I'll like to know if is there a SIMPLE way around my problem, meaning having @PrePersist or @PreUpdate or even other workaround to set the lastModified field still using session
To my knowledge, there is no simple way around (if you're using Spring, MAYBE have a look at this post though).
My suggestion for an Hibernate based solution would be to use events (and one or more interface(s)). Check Hibernate and last modified date for an example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With