在许多方面使用复合键来休眠一对多映射

时间:2012-12-08 23:16:45

标签: hibernate one-to-many composite-key

我面临着Hibernate映射的以下问题 - 花了很多时间做研究,但没有发现类似的情况。让我描述一下我们正在处理的简单数据库模式。

create table point(
POINT_ID integer primary key,
x integer,
y integer);

create table segment(
POINT_A_ID integer references point(POINT_ID),
POINT_B_ID integer references point(POINT_ID),
constraint segment_PK primary key(POINT_A_ID,POINT_B_ID)
);

如您所见,Segment只是两个点之间的一条线,由该对标识(POINT_A_ID,POINT_B_ID)。 我只想从Point的侧面映射这个关联。我发现我应该有两个收藏品,例如 private Set segmentsA,segmentsB;

以下是我的xml映射文件:

<hibernate-mapping>
 <class name="entities.model.Point" table="POINT">
    <id name="id" type="java.lang.Long">
        <column name="POINT_ID" />
        <generator class="native" />
    </id>
    <property name="x" type="int">
        <column name="X" />
    </property>
    <property name="y" type="int">
        <column name="Y" />
    </property>
    <set name="segmentsA" table="SEGMENT" lazy="false" cascade="all-delete-orphan" >
        <key column="POINT_A_ID" not-null="true" update="false" />
        <one-to-many class="entities.model.Segment"/>
    </set>
    <set name="segmentsB" table="SEGMENT" lazy="false" cascade="all-delete-orphan">
        <key column="POINT_B_ID" not-null="true" update="false" />
        <one-to-many class="entities.model.Segment" />
    </set>
 </class>
</hibernate-mapping>

<hibernate-mapping>
<class name="entities.model.Segment" table="SEGMENT">
    <composite-id name="segmentPK" class="entities.model.SegmentPK">
        <key-property name="pointAId" >
            <column name="POINT_A_ID" />
        </key-property>
        <key-property name="pointBId"> 
            <column name="POINT_B_ID"/>
        </key-property>
    </composite-id>
</class>
</hibernate-mapping>

当我尝试在它们之间保留一些Points和Segments时(将一个Segment实例添加到segmentsA和segmentsB组适当的Point实例),我收到以下错误: org.hibernate.MappingException: Repeated column in mapping for entity: entities.model.Segment column: POINT_A_ID (should be mapped with insert="false" update="false")

我非常感谢您对此案的建议:)

0 个答案:

没有答案