java – Hibernate saveOrUpdate()尝试在应该更新时保存

前端之家收集整理的这篇文章主要介绍了java – Hibernate saveOrUpdate()尝试在应该更新时保存前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个名为IssueParticipant的Hibernate实体.它基本上描述了用户和问题之间的关系(类似于JIRA或Bugzilla问题).它表示数据库中的一种多对多链接表,将用户ID链接到问题ID,但还包括通知设置相关的其他信息,因此将其视为自己的实体.

我使用userId和issueId作为复合键时遇到了很大的问题,因此我创建了一个合成键,它是一个String(和postgres数据库中的varchar),形成如下:_.

现在,我有一个屏幕,用户可以编辑与问题相关的所有用户,同时还可以编辑通知设置.在控制器类中,我创建一个IssueParticipants列表,如下所示:

IssueParticipant participant = new IssueParticipant();
participant.setUser(accountUser);
participant.setIssue(issue);

所以这些当然不是由Hibernate管理的.

然后在我的DAO中,我遍历它们并调用saveOrUpdate(),期望如果数据库中存在具有相同合成密钥的IssueParticipant,它将更新;否则将被插入:

for (IssueParticipant participant : participants) {
        getCurrentSession().saveOrUpdate(participant);
        savedIds.add(participant.getIssueUserKey());
    }

(savedIds是我正在维护的列表,以便稍后我将知道我应该从数据库删除哪些IssueParticipants).

而不是我期望的,我得到一个例外:

org.postgresql.util.PsqlException: ERROR: duplicate key value violates unique constraint "issue_participant_pkey"

这是我的实体类,缩写为:

public class IssueParticipant extends Entity {

    private String issueUserKey;
    private Long issueId;
    private Long userId;

     // Edit: adding 'dateAdded' definition
    private Date dateAdded;
// ...

    // below may be null
    private SPUser user;
    private Issue issue;

    public static IssueParticipant nulledIssueParticipant() {
        IssueParticipant ip = new IssueParticipant();
        return ip;
    }
    public String getIssueUserKey() {
        return issueUserKey;
    }

    public void setIssueUserKey(String issueUserKey) {
        this.issueUserKey = issueUserKey;
    }

    public Long getId() {
        // currently meaningless
        return 0L;
    }

    public Long getIssueId() {
        return this.issueId;
    }

    public void setIssueId(Long issueId) {
        this.issueId = issueId;
        updateKey();
    }

    public Long getUserId() {
        return this.userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
        updateKey();
    }

    private void updateKey() {
        issueUserKey = getIssueId() + KEY_SEP + getUserId();
    }

    public SPUser getUser() {
        return user;
    }

    public void setUser(SPUser user) {
        this.user = user;
        setUserId(user.getId());
    }

    public Issue getIssue() {
        return issue;
    }

    public void setIssue(Issue issue) {
        this.issue = issue;
        setIssueId(issue.getId());
    }

// edit: adding 'dateAdded' methods
public Date getDateAdded() {
    return dateAdded;
}

public void setDateAdded(Date dateAdded) {
    this.dateAdded = dateAdded;
}

...

}

这是它的hbm文件

<?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 default-lazy="false">
    <class name="com.xxx.yyy.IssueParticipant" table="issue_participant">
        <id name="issueUserKey" column="issue_user_key" type="string">
            <generator class="assigned"/>
        </id> 
        <version name="dateAdded" column="date_added" type="timestamp" unsaved-value="null" />
        <property name="issueId" column="issue_id" />
        <many-to-one name="user" column="user_id" class="com.xxx.yyy.SPUser" not-null="true" cascade="none" />
        <property name="alertRSS" column="alert_RSS" type="boolean" />
        <property name="alertEmail" column="alert_email" type="boolean" />
        <property name="alertWeb" column="alert_web" type="boolean" />
        <property name="alertClient" column="alert_client" type="boolean" />

    </class>
</hibernate-mapping>

实际上user_issue_key是相应数据库表中的主键.

在这种情况下,我觉得正确的解决方案可能就是使用SpringJDBC,但我真的很想知道这里发生了什么.有人有什么想法?提前致谢.

解决方法

saveOrUpdate()不查询数据库以决定是否应保存或更新给定实体.它根据实体的状态做出决定,如下:
  • if the object is already persistent in this session,do nothing
  • if another object associated with the session has the same identifier,throw an exception
  • if the object has no identifier property,save() it
  • if the object’s identifier has the value assigned to a newly instantiated object,save() it
  • if the object is versioned by a <version> or <timestamp>,and the version property value is the same value assigned to a newly instantiated object,save() it
  • otherwise update() the object

因此,据我所知,在您的情况下,决策是基于dateAdded字段的值,因此您需要保持它以区分新实例和分离实例.

也可以看看:

> 11.7. Automatic state detection

原文链接:https://www.f2er.com/java/123001.html

猜你在找的Java相关文章