File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Diference between SQL Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Diference between SQL "UPDATE" query and Session.saveorUpdate()" Watch "Diference between SQL "UPDATE" query and Session.saveorUpdate()" New topic
Author

Diference between SQL "UPDATE" query and Session.saveorUpdate()

Vamsi Mohan Pemmaraju
Greenhorn

Joined: Jul 15, 2007
Posts: 14
Hi,

In hibernate updation of data is done using session.save() or session.saveOrUpdate().

In addition in Hibernate there is a provision for executing queries explicitly using session.createQuery().

After using session.saveOrUpdate() or the other method I found hibernate executing a select query and then a update or delete query.

Which method is preferred? I suppose the first way will be causing an overhead to the DB.

regards,
Vamsi Mohan
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16624

Simply put, if you use createSQLQuery and write the query yourself, then your code will have to determine whether you need an update statement or an insert statement, it will also not do anything to your related objects within the main object, you will have to manually go through any Collections within the object, and like the main object, determine if you need an insert/update or delete statement, etc. Also, you will have no idea if you have a stale object, which means you will probably want to go to the database first and see if it is stale. So basically you have to do tons of work manually yourself, if you want that control.

With saveOrUpdate method Hibernate will determine whether it needs an insert or update statement, Hibernate will do transitive persistence to your Collections, if you use the corresponding cascade-option. And if you have optimistic locking then it will also do a select for you before the update to determine if you have stale data or not.

So, I'd say for a complex graph of objects, that you are looking at 15 minutes of coding, versus 2-3 days of coding and also easier to maintain and fix bugs because you will a ton less code using saveOrUpdate().

I hope that helps.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
 
subject: Diference between SQL "UPDATE" query and Session.saveorUpdate()
 
Threads others viewed
problem in insert query in hibernate
id value should be sequence or asssigned
Storing and using detached objects in Hibernate
How to update multiple columns in Hibernate(using HQL query)?
Hibernate XDoclet Set Refresh
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture