0

我的休眠事务有问题。执行后,表中没有值,根本没有更新。我所有的其他更新都正常工作。

这是映射

 <class name="LastDownloadedMessage" table="t_imap_lastmsguid">
    <id name="id" column="id" ><generator class="increment"/></id>
    <property name="lastDownloadedMessageUid"><column name="last_msg_uid" /></property>
    <property name="lastUidNext"><column name="next_msg_uid" /></property>
    <property name="folder"><column name="folder_name" /></property>
     <property name="cred"><column name="credential" /></property>
 </class>

这是 POJO 对象:

public class LastDownloadedMessage {
    Integer id;
    private String lastDownloadedMessageUid;
    private String lastUidNext;
    private String folder;
    private String cred;

    //GETTERS AND SETTERS HERE

    public LastDownloadedMessage() {
        super();
    }
    public LastDownloadedMessage(String lastDownloadedMessageUid,
            String lastUidNext) {
        super();
        this.lastDownloadedMessageUid = lastDownloadedMessageUid;
        this.lastUidNext = lastUidNext;
    }
}

这是进行更新的功能。

    Session ssn=HibernateSessionFactory.openSession();
    Transaction txn = ssn.beginTransaction();
    Query query = ssn.createQuery("update LastDownloadedMessage e set e.lastDownloadedMessageUid = :luid , e.lastUidNext = :nextUid where e.folder =:folder and e.cred = :cred");
    query.setParameter("luid",last_downloaded_msg_uid);
    query.setParameter("nextUid", uid_next);
    query.setParameter("folder", folder);
    query.setParameter("cred", credential);
    int result = query.executeUpate();
    txn.commit();
    ssn.flush();
    ssn.close();

该函数似乎可以正常执行,没有错误。可能是什么问题 ?

4

1 回答 1

1

Based on the comments, it seems to me you are confusing UPDATE with INSERT. If result is zero, it means your WHERE clause didn't match anything, so there's nothing to update.

Also, why are you trying to issue an UPDATE/INSERT on a known (mapped) entity? All you have to do is set your values on your object (LastDownloadedMessage) and then execute a persist on your EntityManager. Browse around the web for EntityManager.persist.

于 2013-08-14T14:44:02.033 回答