HibernateTemplate.find()抛出NonUniqueObjectException

时间:2013-07-18 17:28:42

标签: spring hibernate exception

我们得到例外:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [gov.usdoj.afms.umc.hibernate.model.OrgLvl3L#gov.usdoj.afms.umc.hibernate.model.OrgLvl3LId@78c0fb45]

问题是我对这个例外的许多帖子的理解是它们都是保存或更新相关的。这不应该是find()的结果吗?

我们并没有真正理解在同一个会话中两次查询同一个数据库行作为两个不同的find()调用的一部分是非法的。我的意思是,那就是L1缓存是FOR,对吗?

(更新) 这是ID类。它会覆盖等于并更新。不知道为什么。它是由其他人写的。

public class OrgLvl3LId implements java.io.Serializable {

private String structureCd;
private String orgLvl1Cd;
private String orgLvl2Cd;
private String orgLvl3Cd;

public OrgLvl3LId() {
}

public OrgLvl3LId(String structureCd, String orgLvl1Cd, String orgLvl2Cd,
        String orgLvl3Cd) {
    this.structureCd = structureCd;
    this.orgLvl1Cd = orgLvl1Cd;
    this.orgLvl2Cd = orgLvl2Cd;
    this.orgLvl3Cd = orgLvl3Cd;
}

public String getStructureCd() {
    return this.structureCd;
}

public void setStructureCd(String structureCd) {
    this.structureCd = structureCd;
}

public String getOrgLvl1Cd() {
    return this.orgLvl1Cd;
}

public void setOrgLvl1Cd(String orgLvl1Cd) {
    this.orgLvl1Cd = orgLvl1Cd;
}

public String getOrgLvl2Cd() {
    return this.orgLvl2Cd;
}

public void setOrgLvl2Cd(String orgLvl2Cd) {
    this.orgLvl2Cd = orgLvl2Cd;
}

public String getOrgLvl3Cd() {
    return this.orgLvl3Cd;
}

public void setOrgLvl3Cd(String orgLvl3Cd) {
    this.orgLvl3Cd = orgLvl3Cd;
}

public boolean equals(Object other) {
    if ((this == other))
        return true;
    if ((other == null))
        return false;
    if (!(other instanceof OrgLvl3LId))
        return false;
    OrgLvl3LId castOther = (OrgLvl3LId) other;

    return ((this.getStructureCd() == castOther.getStructureCd()) || (this
            .getStructureCd() != null
            && castOther.getStructureCd() != null && this.getStructureCd()
            .equals(castOther.getStructureCd())))
            && ((this.getOrgLvl1Cd() == castOther.getOrgLvl1Cd()) || (this
                    .getOrgLvl1Cd() != null
                    && castOther.getOrgLvl1Cd() != null && this
                    .getOrgLvl1Cd().equals(castOther.getOrgLvl1Cd())))
            && ((this.getOrgLvl2Cd() == castOther.getOrgLvl2Cd()) || (this
                    .getOrgLvl2Cd() != null
                    && castOther.getOrgLvl2Cd() != null && this
                    .getOrgLvl2Cd().equals(castOther.getOrgLvl2Cd())))
            && ((this.getOrgLvl3Cd() == castOther.getOrgLvl3Cd()) || (this
                    .getOrgLvl3Cd() != null
                    && castOther.getOrgLvl3Cd() != null && this
                    .getOrgLvl3Cd().equals(castOther.getOrgLvl3Cd())));
}

public int hashCode() {
    int result = 17;

    result = 37
            * result
            + (getStructureCd() == null ? 0 : this.getStructureCd()
                    .hashCode());
    result = 37 * result
            + (getOrgLvl1Cd() == null ? 0 : this.getOrgLvl1Cd().hashCode());
    result = 37 * result
            + (getOrgLvl2Cd() == null ? 0 : this.getOrgLvl2Cd().hashCode());
    result = 37 * result
            + (getOrgLvl3Cd() == null ? 0 : this.getOrgLvl3Cd().hashCode());
    return result;
}

}

(更新) 来自hmm.xml的片段。您可以看到复合ID定义。

    <composite-id name="id" class="gov.usdoj.afms.umc.hibernate.model.OrgLvl3LId">
        <key-property name="structureCd" type="string">
            <column name="STRUCTURE_CD" length="2" />
        </key-property>
        <key-property name="orgLvl1Cd" type="string">
            <column name="ORG_LVL1_CD" length="5" />
        </key-property>
        <key-property name="orgLvl2Cd" type="string">
            <column name="ORG_LVL2_CD" length="6" />
        </key-property>
        <key-property name="orgLvl3Cd" type="string">
            <column name="ORG_LVL3_CD" length="12" />
        </key-property>
    </composite-id>

0 个答案:

没有答案