How can I enable the Hibernate event listeners, that handle JPA callbacks?
Currently I am using using Hibernate 4 with SessionFactory configuration, but JPA callback
Add a file named org.hibernate.integrator.spi.Integrator containing a single line
org.hibernate.jpa.event.spi.JpaIntegrator
in your META-INF/services folder. This will enable JPA lifecycle annotations including @EntityListeners for sessionFactory based configurations.
This question basically asked the same.
So it turns out, that these JPA entity listener annotations are only working when you are using EntityManager
in Hibernate (which is understandable). So if you want to use these annotations, you should ditch SessionFactory
, and use the JPA-complaint EntityManager
or EntityManagerFactory
.
I also recommend this approach, because if you are trying to use JPA annotations, it is logical to go for a pure JPA solution, without tying yourself to a Hibernate-specific solution - i.e. SessionFactory
.