Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Hibernate 4.3 with SessionFactory to use @PrePersist

Tags:

hibernate

I am trying do it throught this blog steps.

http://leakfromjavaheap.blogspot.com.es/2013/08/prepersist-and-preupdate-not-working.html

But starting from Hibernate 4.3, events package in hibernate-entitymanager.jar are removed.

In another hand, I had been reading about interceptors and events. http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#events

Are this only two ways to implement @PrePersist behaviour? or could use @EntityListeners annotation with SessionFactory?. Although I would prefer win @PrePersist annotation compatibility.

Thank you in advance.

like image 688
Dani Avatar asked Jun 07 '14 11:06

Dani


1 Answers

With Hibernate 4, you can use Integerator spi approach.

Although the hibernate team suggest to use JPA EntityManager, but sometime you just want to keep using the good old SessionFactory with JPA annotations.

  1. Include org.hibernate:hibernate-entitymanager as dependency (assuming you are using maven, pom snippet as follows):

    <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-entitymanager</artifactId>
         <version>4.*</version>
    </dependency>

  2. Create hibernate integrator register file /META-INF/services/org.hibernate.integrator.spi.Integrator and register JpaIntegrator to enable JPA Event Annotations by pasting following content:

    org.hibernate.jpa.event.spi.JpaIntegrator

Ref:
arkuarku.wordpress.com/2014/10/23/spring-hibernate4-enable-jpa-prepersistpreupdate-annotation-using-sessionfactroy/
See:
https://docs.oracle.com/javase/tutorial/sound/SPI-intro.html
http://in.relation.to/2012/01/09/event-listener-registration/

like image 68
langlan4744 Avatar answered Nov 09 '22 22:11

langlan4744