Configure Hibernate 4.3 with SessionFactory to use @PrePersist

I am trying to do this through the steps of this blog.

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

But starting with Hibernate 4.3, the event package in hibernate-entitymanager.jar is deleted.

In the other hand, I read about interceptors and events. http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#events

Are these just two ways to implement @PrePersist behavior? or can use the @EntityListeners annotation with SessionFactory ?. Although I would prefer win @PrePersist compatible with annotation.

Thanks in advance.

+6
source share
1 answer

With Hibernate 4, you can use the Integerator spi approach.

Although the sleep mode command suggests using the JPA EntityManager, sometimes you just want to use the good old SessionFactory with JPA annotations.

  • Include org.hibernate: hibernate-entitymanager as a dependency (assuming you use maven, pom snippet as follows):

    <dependence>
    <group id> org.hibernate </ group id>
    <artifact> hibernation EntityManager - </ artifact>
    <version> 4 * <./ Version>
    </ dependency>

  • Create an integrator integrator register file /META-INF/services/org.hibernate.integrator.spi.Integrator and register JpaIntegrator to enable JPA event annotations by pasting the following content:

    org.hibernate.jpa.event.spi.JpaIntegrator

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

+4
source

Source: https://habr.com/ru/post/970503/


All Articles