Does Hibernate's GenericGenerator cause update and saveOrUpdate to always insert instead of update?

Posted by Derek Mahar on Stack Overflow See other posts from Stack Overflow or by Derek Mahar
Published on 2010-06-02T19:55:31Z Indexed on 2010/06/02 20:34 UTC
Read the original article Hit count: 329

Filed under:
|
|

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);
    }
}

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about update