Reputation: 1691
I used Spring's HibernateTemplate to save entity, I also add call back method like this
@PrePersist
public void prePersist() {
setCreateDate(new Date());
}
but I found this callback annotation was not called when I called saveOrUpdate() method.
public void persist(Object entity) {
hibernateDaoSupport.getHibernateTemplate().saveOrUpdate(entity);
}
I found some post said that only use EntityManager will call these callback annotation method, is it right? If not, why my @PrePersist not being called. Could anybody give me a direction to investigate the problem, thanks a lot.
Upvotes: 1
Views: 2570
Reputation: 100726
Yes, EntityManager event listener methods are only invoked if you're using EntityManager. You should be using JPA template instead of HibernateTemplate within Spring if you want to use JPA instead of raw Hibernate.
Upvotes: 4