What's the difference between session.persist() and session.save() in Hibernate?
From this forum post
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 withpersist()
.
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.
Whats the difference between persist() and save() in Hibernate?
I did some mock testing to record the difference between Save() and Persist().
Sounds like both these methods behaves same when dealing with Transient Entity but differ when dealing with Detached Entity.
For the below example , take EmployeeVehicle as an Entity with PK as vehicleId which is a generated value and vehicleName as one of its property .
Example 1 : Dealing with Transient Object
Session session = factory.openSession();
session.beginTransaction();
EmployeeVehicle entity = new EmployeeVehicle();
entity.setVehicleName("Honda");
session.save(entity);
// session.persist(entity);
session.getTransaction().commit();
session.close();
Result : select nextval ('hibernate_sequence')
// This is for vehicle Id generated : 36
insert into Employee_Vehicle ( Vehicle_Name, Vehicle_Id) values ( Honda, 36)
Repeat the same with using persist(entity) and will result the same with new Id ( say 37 , honda ) ;
Example 2 : Dealing with Detached Object
// Session 1
// Get the previously saved Vehicle Entity
Session session = factory.openSession();
session.beginTransaction();
EmployeeVehicle entity = (EmployeeVehicle)session.get(EmployeeVehicle.class, 36);
session.close();
// Session 2
// Here in Session 2 , vehicle entity obtained in previous session is a detached object and now we will try to save / persist it
(i) Using Save() to persist a detached object
Session session2 = factory.openSession();
session2.beginTransaction();
entity.setVehicleName("Toyota");
session2.save(entity);
session2.getTransaction().commit();
session2.close();
Result : You might be expecting the Vehicle with id : 36 obtained in previous session is updated with name as "Toyota" . But what happens is that a new entity is saved in the DB with new Id generated for and Name as "Toyota"
select nextval ('hibernate_sequence')
insert into Employee_Vehicle ( Vehicle_Name, Vehicle_Id) values ( Toyota, 39)
(ii) Using Persist() to persist a detached object
// Session 1
Session session = factory.openSession();
session.beginTransaction();
EmployeeVehicle entity = EmployeeVehicle)session.get(EmployeeVehicle.class, 36);
session.close();
// Session 2
// Here in Session 2 , vehicle entity obtained in previous session is a detached object and now we will try to save / persist it
(i) Using persist() to persist a detached object
Session session2 = factory.openSession();
session2.beginTransaction();
entity.setVehicleName("Toyota");
session2.persist(entity);
session2.getTransaction().commit();
session2.close();
Result : Exception being thrown : detached entity passed to persist
So, it is always better to use Persist() rather than Save() as save has to be carefully used when dealing with session and transcation .
What exactly happens when I use session.save instead of session.persist?
In your example, tempCourse1
and tempCourse2
are in a detached state, so to make a relation you need to persist them.
Both save
and persist
doing the same thing, but save
is Hibernate's API and persist
is a part of the JPA specification.
On your entity, you have CascadeType.PERSIST
which relates only to the persist
method. To make save
behave the same, you should add org.hibernate.annotations.CascadeType#SAVE_UPDATE
to your ManyToOne
annotation.
What are the differences between the different saving methods in Hibernate?
Here's my understanding of the methods. Mainly these are based on the API though as I don't use all of these in practice.
saveOrUpdate
Calls either save or update depending on some checks. E.g. if no identifier exists, save is called. Otherwise update is called.
save
Persists an entity. Will assign an identifier if one doesn't exist. If one does, it's essentially doing an update. Returns the generated ID of the entity.
update
Attempts to persist the entity using an existing identifier. If no identifier exists, I believe an exception is thrown.
saveOrUpdateCopy
This is deprecated and should no longer be used. Instead there is...
merge
Now this is where my knowledge starts to falter. The important thing here is the difference between transient, detached and persistent entities. For more info on the object states, take a look here. With save & update, you are dealing with persistent objects. They are linked to a Session so Hibernate knows what has changed. But when you have a transient object, there is no session involved. In these cases you need to use merge for updates and persist for saving.
persist
As mentioned above, this is used on transient objects. It does not return the generated ID.
What exactly does org.hibernate.Session.save() do?
Difference between save
and saveOrUpdate
Main difference between save
and saveOrUpdate
method is that save
generates a new identifier and INSERT
record into database while saveOrUpdate
can either INSERT
or UPDATE based upon existence of record. So save
will proceed without performing existence check, on the other hand saveOrUpdate
will check for existence, if record exists it will be updated else a new record will be inserted.
Basic differences between persist
and save
1)First difference between save
and persist
is their return
type. Similar to save
method, persist
also INSERT
records into database but return type of persist
is void
while return type of save
is Serializable object
.
2) Another difference between persist
and save
is that both methods make a transient instance persistent. However, persist
method doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time.
Related Topics
Getresourceasstream() Is Always Returning Null
What to Learn for Making Java Web Applications in Java Ee 6
How to Programmatically Enable Gps in Android Cupcake
Is Custom Enum Serializable Too
Opencv Template Matching Example in Android
Android Httppost: How to Get the Result
Broadcast Receiver as Inner Class in Android
How to Include Jar Dependency into an Aar Library
Alertdialog from Within Broadcastreceiver? Can It Be Done
How to Limit the Number of Characters in Jtextfield
How to Initialize List<String> Object in Java
Can CSS in Javafx Be Used for Animations
Screen Video Record of Current Activity Android
What's a Good Library for Parsing Mathematical Expressions in Java
How to Send Data from Dialogfragment to a Fragment