35
votes

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

2

2 Answers

49
votes

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.

-1
votes

Mainly @PrePersist and @PreUpdate annotations are used in entity in hibernate or jpa. When we save an entity on that time @PrePersist method is called.