Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@PreUpdate and @Prepersist in hibernate/JPA (using session)

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

like image 790
black sensei Avatar asked Nov 09 '10 11:11

black sensei


People also ask

What is @PrePersist in JPA?

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.

What is PrePersist and PreUpdate?

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.

What is @PrePersist and @PostPersist?

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.

What is PreUpdate annotation?

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.


1 Answers

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.

like image 187
Pascal Thivent Avatar answered Oct 02 '22 23:10

Pascal Thivent