问题
How does one turn on the required Spring / JPA / Hibernate behaviours to invoke @PrePersist and @PreUpdate hooks?
Our stack includes those three and a Repository for each entity, but we feel it is better to model this as a resposibility of the entity itself with the help of these hooks, but they are not called. Each Repository has an EntityManager injected. I am using those EntityManagers, not Sessions.
The EntityManager is created by a org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean which uses the org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter and org.springframework.orm.jpa.JpaTransactionManager. There is a org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor present. The factory references a com.mchange.v2.c3p0.ComboPooledDataSource.
Is it necessary to manually install / wire the something in (an entity listener?) to ensure the calls are made. I cannot locate any documentation for this.
来源:https://stackoverflow.com/questions/14458473/enable-prepersist-and-preupdate-in-spring