8

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.

Dani
  • 4,001
  • 7
  • 36
  • 60
  • Hi Dani, i am also struggling around the same problem. Did you find anythng regarding jpa callback handlers? – Dheeraj Arora Nov 09 '14 at 21:10
  • Hi dheerajarora, There is nothing to do on this way, @PrePersist just is compatible with EntityManager. I had to play with the Transaction manually for that purpose. Regards. – Dani Nov 10 '14 at 15:38

2 Answers2

8

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/

langlan4744
  • 91
  • 1
  • 5
  • Linking to an external site will make this answer outdated as soon as the link is broken. Is it in any way possible to describe the approach here? Only keep the link here as an extra reference. – Bryan Oemar Nov 25 '15 at 19:46
  • @langlan4744: I just tested this with Hibernate 4.2 and instead of putting org.hibernate.jpa.event.spi.JpaIntegrator in the file, you should put org.hibernate.ejb.event.JpaIntegrator in. This solution works beautifully! Thanks!! – sudokai May 05 '16 at 15:22
  • 1
    This also works with hibernate 5. Also ensure that the imported `hibernate-entitymanager` version is the same as your `hibernate-core` version, to avoid cryptic errors! – davnicwil Aug 12 '16 at 08:15
0

If you really need to continue working with SessionFactory - here is a working example: Github link

SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) sessionFactoryBean.getObject();
SessionFactoryServiceRegistry serviceRegistry = (SessionFactoryServiceRegistry) sessionFactory.getServiceRegistry();
Configuration configuration = sessionFactoryBean.getConfiguration();

new JpaIntegrator().integrate(configuration, sessionFactory, serviceRegistry);

Note: don't forget to add hibernate-entitymanager dependency.

Oleksandr
  • 450
  • 1
  • 6
  • 13