0

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

请参考以下代码: 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>

用户.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
    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");
    }

在设置中,我正在创建 Presenter1,如下所示:

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

        userDao.create(presenter1);

请让我知道它为什么不更新实体。

4

2 回答 2

0

Why you declared tx.commit(); for commit? the correct declaration for commit is: session.getTransaction().commit();

Bye

于 2012-10-07T17:52:44.900 回答
0

当我发布这个问题时,我才意识到 update=false 是在映射文件中配置的。我为这个错误浪费了很多时间。:( 现在,我将其从映射文件中删除并使其正常工作。

于 2012-10-07T08:09:51.130 回答