Alex Rivera | Logout

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

Asked 2010-05-18T13:05:55.130
16

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.

Edit
Report

1 Answer

-2

You should use session.update(object) while you are updating, and session.save(object) while you are saving

answered 2010-05-18T13:32:16.037

Your Answer