Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails when to use merge() instead of save()?

Hello any one suggest me that when to use merge() instead of save()?

I have some issue like when I save domain with save() method it's working fine in some case but in some case it's creating "org.hibernate.LazyInitializationException" exception for me.

Can I use direct merge() method to save domain instance instead of save()?

like image 938
Piyush Chaudhari Avatar asked Nov 06 '14 07:11

Piyush Chaudhari


1 Answers

You can see that both merge() and save() methods calling save() method internally, the difference is in their behavior i.e If you store object in HTTP session and later want to access that but this object has lost its persistent context, the merge() method merge's a detached object's state back into the current Hibernate session.But if you called the save() method on detached object's you will face a sexy exception(org.hibernate.LazyInitializationException) as mentioned by you. For further info documentation.

like image 78
Abs Avatar answered Oct 23 '22 20:10

Abs