2

我在 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 一侧映射此关联。我发现我应该有两个集合,例如私有 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>

当我尝试在它们之间保留一些点和线段时(已将一个 Segment 实例添加到 SegmentA 和 SegmentB 适当的 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")

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

4

0 回答 0