0

User エンティティとその更新を更新しようとしています:

以下のコードを参照してください: Hibernate Mapping File:

<?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

この質問を投稿したとき、マッピングファイルにupdate=falseが設定されていることに気づきました。私はこの間違いのために多くの時間を無駄にしました。:(今、私はそれをマッピングファイルとその動作から削除しました。

于 2012-10-07T08:09:51.130 に答える
0

tx.commit(); を宣言した理由 コミットする?commit の正しい宣言は、session.getTransaction().commit(); です。

さよなら

于 2012-10-07T17:52:44.900 に答える