File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes difference between save() and persist() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "difference between save() and persist() method" Watch "difference between save() and persist() method" New topic

difference between save() and persist() method

swe ka

Joined: Aug 02, 2009
Posts: 3
the one thing i know is save() method will return the identifier and persist() will not. Apart from that is there any difference in using these two methods??
swe ka

Joined: Aug 02, 2009
Posts: 3
well, i found the answer from a forum

just in case if some one else gets this doubt.

Here is the answer

"persist() is well defined. It makes a transient instance persistent. However, it doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. The spec doesn't say that, which is the problem I have with persist().

persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context.

A method like persist() is required.

save() does not guarantee the same, it returns an identifier, and if an INSERT has to be executed to get the identifier (e.g. "identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or outside of a transaction. This is not good in a long-running conversation with an extended Session/persistence context."
I agree. Here's the link:
subject: difference between save() and persist() method
Similar Threads
EJB3.0 - persist() vs create()
Using Oracle sequence with JBOSS
New Entity with an existing primary key.
How I can get the primary key assigned to an object when I use em.persist method?
Hibernate Persistence