Java 保存子对象列表时发生org.hibernate.unUniqueObjectException

Java 保存子对象列表时发生org.hibernate.unUniqueObjectException,java,hibernate,hibernate-mapping,Java,Hibernate,Hibernate Mapping,创建父对象时,我需要保存几个子对象。理想情况下,我希望将它们保存在一个事务中,但是,我不断得到一个org.hibernate.ununiqueObjectException:具有相同标识符值的不同对象已经与会话关联 在本例中,一个子学科有许多子学科图层 我觉得地图有问题 POJO: Subdiscipline.java Main.java: 尝试第二次保存时,请使用“合并”而不是“保存” sd.setSubdisciplinePlayers(new ArrayList<Subdisc

创建父对象时,我需要保存几个子对象。理想情况下,我希望将它们保存在一个事务中,但是,我不断得到一个
org.hibernate.ununiqueObjectException:具有相同标识符值的不同对象已经与会话关联

在本例中,一个子学科有许多子学科图层

我觉得地图有问题

POJO: Subdiscipline.java Main.java:
尝试第二次保存时,请使用“合并”而不是“保存”

   sd.setSubdisciplinePlayers(new ArrayList<SubdisciplinePlayer>());
   sd.getSubdisciplinePlayers().add(sdp);
   sd.getSubdisciplinePlayers().add(sdp2);
   session.merge(sd);
sd.setSubStructurePlayers(新的ArrayList());
sd.getsubdisciplineplyers().add(sdp);
sd.getsubdisciplineplyers().add(sdp2);
合并会议(sd);
我为你列出了更多的参考资料


您已经为您的实体
子ScilinePlayer
声明了带有字段
纪律ID
子ScilineID
的复合键

现在,在您的程序中,您正在创建两个具有相同复合id的
子SciplinePlayer
实例

SubdisciplinePlayer sdp = new SubdisciplinePlayer();
sdp.setDisciplineId(sd.getDisciplineId());
sdp.setSubDisciplineId(sd.getId());

SubdisciplinePlayer sdp2 = new SubdisciplinePlayer();
sdp2.setDisciplineId(sd.getDisciplineId());
sdp2.setSubDisciplineId(sd.getId());
因此,当您调用
Session.save(sd)
时,hibernate观察到您正试图保存
子SciplinePlayer
的两个实例,并将异常抛出为:

org.hibernate.NonUniqueObjectException: a different objectwith the same identifier 
value was already associated with the session:     
[net.viralpatel.hibernate.SubdisciplinePlayer#
net.viralpatel.hibernate.SubdisciplinePlayer@1cd7e9c3]

要解决此问题,只需确保
子ScilinePlayer
的复合id应具有不同的值,因为主键应是唯一的。

现在您已经指出了这一点,这似乎很明显,谢谢。问题在于,idPlayers字段是一个自动递增字段,是子SciplinePlayers复合键的一部分。如何修改映射以反映这一点?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="net.viralpatel.hibernate.Subdiscipline" table="sub_discipline">
        <id name="id" column="sub_dis_id" unsaved-value="0">
            <generator class="increment"/>
        </id>
        <property name="disciplineId" column="dis_id" type="int"/>
        <property name="name" column="sub_discipline" type="string"/>           
        <bag name="subdisciplinePlayers" table="sdis_players" inverse="true" cascade="all">
            <key column="sub_dis_id" />
            <one-to-many class="net.viralpatel.hibernate.SubdisciplinePlayer" />
        </bag>

    </class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="net.viralpatel.hibernate.SubdisciplinePlayer" table="sdis_players">
        <composite-id>
            <key-property name="disciplineId" column="dis_id" type="int"/>
            <key-property name="subDisciplineId" column="sub_dis_id" type="int" />
        </composite-id>
        <property name="idPlayers" column="id_players" type="int"/>
        <property name="players" column="players" type="int"/>
        <property name="minPlayers" column="min_players" type="int"/>
        <property name="maxPlayers" column="max_players" type="int"/>
        <many-to-one name="subdiscipline" class="net.viralpatel.hibernate.Subdiscipline" cascade="none" insert="false" update="false">
            <column name="sub_dis_id"/>
        </many-to-one>
    </class>
</hibernate-mapping>
Exception in thread "main" org.hibernate.NonUniqueObjectException: a different object     with the same identifier value was already associated with the session:     [net.viralpatel.hibernate.SubdisciplinePlayer#net.viralpatel.hibernate.SubdisciplinePlayer@1cd7e9c3]
   sd.setSubdisciplinePlayers(new ArrayList<SubdisciplinePlayer>());
   sd.getSubdisciplinePlayers().add(sdp);
   sd.getSubdisciplinePlayers().add(sdp2);
   session.merge(sd);
SubdisciplinePlayer sdp = new SubdisciplinePlayer();
sdp.setDisciplineId(sd.getDisciplineId());
sdp.setSubDisciplineId(sd.getId());

SubdisciplinePlayer sdp2 = new SubdisciplinePlayer();
sdp2.setDisciplineId(sd.getDisciplineId());
sdp2.setSubDisciplineId(sd.getId());
org.hibernate.NonUniqueObjectException: a different objectwith the same identifier 
value was already associated with the session:     
[net.viralpatel.hibernate.SubdisciplinePlayer#
net.viralpatel.hibernate.SubdisciplinePlayer@1cd7e9c3]