views:

572

answers:

1

Hi 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.

+2  A: 

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.

ChssPly76
Is there a Hibernate equivalent to `@PrePersist` that can be used with the `HibernateTemplate`?
Marcus