Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using saveOrUpdate() in Hibernate creates new records instead of updating existing ones

I have a class User

class User { 
  int id;
  String name;
}

where id is native generator in User.hbm.xml and name is primary-key in DB.

In my database I saved some information about Users.

Than I want to connect with this information about User.

For example in my DB I have a row INSERT INTO User VALUES ('Bill');

Main.java

User bill = new User();
bill.setName("Bill");
session.saveOrUpdate(bill);

This code always tries to insert a new Bill row into the table rather than update the existing Bill row.

like image 707
kunkanwan Avatar asked May 18 '10 13:05

kunkanwan


People also ask

How does saveOrUpdate work in hibernate?

When you use . saveOrUpdate() Hibernate will check if the object is transient (it has no identifier property) and if so it will make it persistent by generating it the identifier and assigning it to session. If the object has an identifier already it will perform .

What is the difference between the Save () and saveOrUpdate () method of hibernate?

Difference between save and saveOrUpdate in Hibernate The main difference between save and saveOrUpdate method is that save() generates a new identifier and INSERT record into the database while saveOrUpdate can either INSERT or UPDATE based upon the existence of a record.

What is difference between Merge and saveOrUpdate in hibernate?

Once save/update is done, the object DOES NOT reflect the change. The returned object reflects the changes, and it is attached to hibernate session. MERGE method offers greater flexibility when it comes to saving data objects, since you need not worry about attaching object to Session.

What is the difference between Save () and persist () method in hibernate?

The save() method provides an identifier with the intent of an insert query being executed immediately for getting the identifier. It does not matter whether it is outside or inside a transaction. The persist() method fails to execute a given insert query in case it is placed outside transaction boundaries.


1 Answers

This code always trying insert bill to database , rather than update when row about Bill exists in DB...

From the section 10.7. Automatic state detection of the Hibernate core documentation:

saveOrUpdate() does the following:

  • if the object is already persistent in this session, do nothing
  • if another object associated with the session has the same identifier, throw an exception
  • if the object has no identifier property, save() it
  • if the object's identifier has the value assigned to a newly instantiated object, save() it
  • if the object is versioned by a <version> or <timestamp>, and the version property value is the same value assigned to a newly instantiated object, save() it
  • otherwise update() the object

When you do:

User bill = new User();
bill.setName("Bill");
session.saveOrUpdate(bill);

This newly created instance does not have any identifier value assigned and saveOrUpdate() will save() it, as documented. If this is not what you want, make the name the primary key.

like image 108
Pascal Thivent Avatar answered Oct 11 '22 12:10

Pascal Thivent