Hibernate - 实体没有得到更新

时间:2012-10-07 08:05:56

标签: hibernate orm hibernate-mapping

我正在尝试更新用户实体及其正在更新:

请参考以下代码: Hibernate映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="sg.edu.nus.iss.phoenix.user.entity.User" table="user">
        <id name="id" column="UserId">
        </id>
        <property name="password" column="Password" type="string"
            update="false" />
        <property name="name" column="Name" type="string"
            update="false" />
        <property name="status" column="Status" type="string" />
    </class>
</hibernate-mapping>

User.java

public class User implements Cloneable, Serializable {

    /**
     * For eclipse based unique identity
     */
    private static final long serialVersionUID = -3737184031423373198L;
    /**
     * Persistent Instance variables. This data is directly mapped to the
     * columns of database table.
     */
    private String id;
    private String password;
    private String name;
//  private UserTypeEnum status;
    private String status;
    private List<Role> roles = new ArrayList<Role>();

    /**
     * Constructors. The first one takes no arguments and provides the most
     * simple way to create object instance. The another one takes one argument,
     * which is the primary key of the corresponding table.
     */

    public User() {

    }

    public User (String userId, String password){
        this.id = userId;
        this.password = password;
        this.roles = new ArrayList<Role>();
    }

    public User (String userId, String password, String name){
        this.id = userId;
        this.password = password;
        this.name = name;
        this.status = "Active";
        this.roles = new ArrayList<Role>();
    }

    public User(String idIn) {

        this.id = idIn;

    }

    /**
     * Get- and Set-methods for persistent variables. The default behaviour does
     * not make any checks against malformed data, so these might require some
     * manual additions.
     */

    public String getId() {
        return this.id;
    }

    public void setId(String idIn) {
        this.id = idIn;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String passwordIn) {
        this.password = passwordIn;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String nameIn) {
        this.name = nameIn;
    }

    public String getStatus() {
        return status;
    }
/*
    public void setStatus(UserTypeEnum status) {
        this.status = status;
    }
    */
    public void setStatus(String status)
    {
        this.status=status;
    }
    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }
}

UserDAOImpl.java

public class UserDaoImpl implements UserDao {

    @Override
    public boolean update(User t) throws SQLException {
        boolean status;
        try{
            Session session  = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.saveOrUpdate(t);
            session.flush();
            session.refresh(t);
            tx.commit();
            session.close();
            status = true;
        }catch (HibernateException e) {
            e.printStackTrace();
            status=false;
        }
        return status;
    }
--------
}

Junit -Test:

@Test
    public void testUpdate() throws SQLException {
        presenter1.setName("PREE");
        boolean status = userDao.update(presenter1);
        List<User> users = userDao.searchMatching(presenter1);
        String name = users.get(0).getName();
        assertEquals(name, "PREE");
    }

在设置中,我正在创建这样的presenter:

// Create user
        presenter1 = new User(PRESENTER1, PASSWORD, PRESENTER_NAME1);
        //session.saveOrUpdate(presenter1);

        userDao.create(presenter1);

请告诉我为什么不更新实体。

2 个答案:

答案 0 :(得分:0)

当我发布这个问题时,我才意识到在映射文件中配置了update = false。我为这个错误浪费了太多时间。 :( 现在,我从映射文件及其工作中删除了它。

答案 1 :(得分:0)

为什么你宣布tx.commit();提交? 正确的提交声明是:session.getTransaction()。commit();

再见