1

我们得到了一个例外:

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 缓存的用途,对吧?

(更新)这是 ID 类。它确实覆盖了equals和更新。不知道为什么。是别人写的。

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 的片段。您可以看到复合标识定义。

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

0 回答 0