Does Hibernate's GenericGenerator cause update and saveOrUpdate to always insert instead of update?
- by Derek Mahar
When using GenericGenerator to generate unique identifiers, do Hibernate session methods update() and saveOrUpdate() always insert instead of update table rows, even when the given object has an existing identifier (where the identifier is also the table primary key)? Is this the correct behaviour?
public class User {
private String id;
private String name;
public User(String id, String name) {
this.id = id;
this.name = name;
}
@GenericGenerator(name="generator", strategy="guid")@Id @GeneratedValue(generator="generator")
@Column(name="USER_ID", unique=true, nullable=false)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
@Column(name="USER_NAME", nullable=false, length=20)
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
class UserDao extends AbstractDaoHibernate {
public void updateUser(final User user) {
HibernateTemplate ht = getHibernateTemplate();
ht.saveOrUpdate(user);
}
}