问题描述:

I'm facing the following problem with Hibernate mapping - spent pretty much time doing a research but haven't found an analogous case. Let me describe the simple database schema we're dealing with.

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)

);

As you can see, a Segment is just a line between two points, identified by the pair (POINT_A_ID, POINT_B_ID).

I only want to have this association mapped from the Point's side. I've figured out I should have two collections, e.g.

private Set segmentsA, segmentsB;

Here are my xml mapping files:

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

and

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

When I'm trying to persist some Points and Segments between them (having added one Segment instance to segmentsA and segmentsB sets of appropriate Point instances), I get the following error:

org.hibernate.MappingException: Repeated column in mapping for entity: entities.model.Segment column: POINT_A_ID (should be mapped with insert="false" update="false")

I'll be very grateful for your advice on this case :)

相关阅读:
Top