Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate saveOrUpdate vs update vs save/persist

I am struggling to apprehend the slight differences between the hibernate methods

saveOrUpdate - update - save/persist.

I know there are some similar questions on the site:

What are the differences between the different saving methods in Hibernate?

Difference between save and saveOrUpdate method hibernate

but having read them, I did not notice an answer covering all the issues coming from using those methods in any case. I would to mention the example I have created to test: I have a table USER with the records:

id     |      company



1             Company1

2             Company2

I execute then the code:

 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
 Transaction tx = session.beginTransaction();

 User user1 = (User) session.load(User.class, Integer.valueOf(1));
 user1.setCompany("Company3");
 User user2 = (User) session.load(User.class, Integer.valueOf(2));
 user2.setCompany("Company4");
 session.persist(user1);
 session.save(user2);

 tx.commit();

I see in the database:

id     |      company



 1             Company3

 2             Company4

I notice that save and persist in this case do the same task as saveOrUpdate or update.My question is therefore what is the diferrence between them and when are saveOrUpdate or update necessary. Am I right that with save or persist the associated objects are not updated even if using Cascade?

like image 667
arjacsoh Avatar asked Sep 07 '14 09:09

arjacsoh


2 Answers

Both save() and persist() are used to insert a new entity in the database. You're calling them on entities that already exist in the database. So they do nothing.

The main difference between them is that save() is Hibernate-proprietary, whereas persist() is a standard JPA method. Additionally, save() is guaranteed to assign and return an ID for the entity, whereas persist() is not.

update() is used to attach a detached entity to the session.

saveOrUpdate() is used to either save or update an entity depending on the state (new or detached) of the entity.

Note that you don't need to call any method of the session to modify an attached entity: doing

User user1 = (User) session.load(User.class, Integer.valueOf(1));
user1.setCompany("Company3");

is sufficient to have the company of the user 1 updated in the database. Hibernate detects the changes made on attached entities, and saves them in the database automatically.

like image 171
JB Nizet Avatar answered Oct 22 '22 15:10

JB Nizet


save Save method stores an object into the database. That means it insert an entry if the identifier doesn’t exist, else it will throw error. If the primary key already present in the table, it cannot be inserted.

update Update method in the hibernate is used for updating the object using identifier. If the identifier is missing or doesn’t exist, it will throw exception.

saveOrUpdate This method calls save() or update() based on the operation. If the identifier exists, it will call update method else the save method will be called. saveOrUpdate() method does the following: If the object is already persistent in the current session, it do nothing If another object associated with the session has the same identifier, throw an exception to the caller If the object has no identifier property, save() the object If the object’s identifier has the value assigned to a newly instantiated object, save() the object - See more at: http://www.javabeat.net/difference-between-hibernates-saveupdate-and-saveorupdate-methods/#sthash.ZwqNlWXH.dpuf

like image 24
user2815238 Avatar answered Oct 22 '22 13:10

user2815238