使用SessionFactory配置Hibernate 4.3以使用@PrePersist

Dan*_*ani 7 hibernate

我正在尝试通过这个博客步骤.

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

但是从Hibernate 4.3开始,hibernate-entitymanager.jar中的事件包被删除了.

另一方面,我一直在阅读有关拦截器和事件的内容. http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#events

这只是实现@PrePersist行为的两种方式吗?或者可以在SessionFactory中使用@EntityListeners注释?虽然我更喜欢win @PrePersist注释兼容性.

先感谢您.

lan*_*744 7

使用Hibernate 4,您可以使用Integerator spi方法.

虽然hibernate团队建议使用JPA EntityManager,但有时你只想继续使用旧的SessionFactory和JPA注释.

  1. 包含org.hibernate:hibernate-entitymanager作为依赖项(假设您正在使用maven,pom片段如下):

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

  2. 创建hibernate集成器寄存器文件/META-INF/services/org.hibernate.integrator.spi.Integrator 并注册JpaIntegrator以通过粘贴以下内容来启用JPA事件注释:

    org.hibernate.jpa.event.spi.JpaIntegrator

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