aspose file tools*
The moose likes Object Relational Mapping and the fly likes PrePersist doesn't work with Hibernate Template Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "PrePersist doesn Watch "PrePersist doesn New topic
Author

PrePersist doesn't work with Hibernate Template

Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
Hi,

I am using Spring's Hibernate template to save the entities.

I tried to use entity callback methods like PostPersist, PreRemove but they are not invoked.

Also, i read somewhere that these methods will work only if we use JPATemplate instead of Hibernate template.

Are there any similar lifecycle methods that we can use while using HibernateTemplate.

Thank you

Rahul


Rahul Babbar
Henrique Ordine
Ranch Hand

Joined: Sep 03, 2004
Posts: 127
Hello Rahul,

I'm afraid I don't have a solution for you, but did you find an alternative? I'm trying the same thing, adding a method anotated with @PrePersist but it doesn't get invoked.

Thanks in advance.


J2EE Architect/Developer
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
Hello Henrique,

I did not get the solution for it, and had to change my API to accomplish it..

Like in my BaseDAOs for saving the entities, i introduced the callbacks like beforeSave(), afterSave() will null implmentation, and overrode those methods in the DAO of whichever entity i needed to save.

I think there might be a more elegant solution for it, but it satisfied my requirement so, i went ahead with that.

Regards,

Rahul
Henrique Ordine
Ranch Hand

Joined: Sep 03, 2004
Posts: 127
Yes, it's a bummer. I've just read on some forums that if I use Session instead of EntityManager the callBack Methods don't get invoked. I can't guarantee that programmers will use EntityManager and not Session.

Thanks for your reply and idea, anyway.
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
Infact, i am probably not sure about the concept of EntityManager.

Since Hibernate Template is provided by Spring, which uses session internally, probably Spring needs to provide a similar wrapper over Hibernate's EntityManager.

Thanks

Rahul
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: PrePersist doesn't work with Hibernate Template