Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PrePersist doesn't work with Hibernate Template

 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henrique Ordine
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic