Hibernate saveOrUpdate vs update vs save / persist

I am struggling to understand the small differences between sleeping methods

saveOrUpdate - update - save/persist

...

I know the site has similar questions:

What are the differences between the various persistence methods in Hibernate?

Difference between hibernate and saveOrUpdate method

but after reading them I didn't notice the answer to all the questions related to using these methods anyway. I would like to mention an example I created for testing: I have a USER table with entries:

id     |      company



1             Company1

2             Company2

      

Then I execute 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, perform the same task as the saveOrUpdate

or update

. So the question is what is the difference between them and when are needed saveOrUpdate

or update

, Is it correct that with save

or persist

related objects are not updated even if used Cascade

?

+3


source to share


3 answers


Both save()

and persist()

are used to insert the object new database. You call them on entities that already exist in the database. So they don't do anything.

The main difference between the two is that it save()

is Hibernate-proprietary whereas it persist()

is a standard JPA method. Also, it is save()

guaranteed to assign and return an ID to an object when it is persist()

not.

update()

used to attach an individual object to a session.

saveOrUpdate()

used to save or update an object depending on the state (new or detached) of the object.



Please note that you do not need to call any session method to change the attached object: do

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

      

enough to update the user database of 1 user in the database. Hibernate detects changes made to attached objects and automatically saves them to the database.

+8


source


save The save method stores an object in the database. This means it inserts a record if the id doesn't exist, otherwise it will throw an error. If the primary key already exists in the table, it cannot be inserted.

Refresh The hibernate refresh method is used to update an object using an identifier. If the identifier is missing or does not exist, it will throw an exception.



saveOrUpdate This method calls save () or update () based on the operation. If the identifier exists, it is called by the update method, otherwise the save method will be called. The saveOrUpdate () method does the following: If the object is already persistent in the current session, it does nothing If another object associated with the session has the same identifier, throws an exception for the caller If the object does not have an identifier property, save () the object If the object id has the value assigned to the newly created object, save () the object - See more at: http://www.javabeat.net/difference-between-hibernates-saveupdate-and-saveorupdate-methods/#sthash.ZwqNlWXH. dpuf

+1


source


saveOrUpdate

- inserts a row if it does not exist in the database, or update it if it exists.

save

- always try to insert a row into the database.

update

- always try to update a row in the database. Usage example saveOrUpdate

.

Suppose you have developed a program that gets information about user visits for the current day from Google Analytics and stores them in your database.

If there is no day information in the database, the method saveOrUpdate

will insert data, otherwise it will update the existing data.

-1


source







All Articles