tags:

views:

105

answers:

2

How should I do if I have two tables and their primary keys have the same name like the following XML mapping file suggests?

<class name="Person" table="person">

    <id name="uid_" type="java.lang.String" column="uid">
     <generator class="native" />
    </id>

    <property name="name_" type="java.lang.String" column="pName" />
    <property name="age_" type="integer" column="age" />
    <property name="birthday_" type="date" column="birthday"/>

    <many-to-one name="school" class="School" column="schoolID" unique="false" />

    <bag name="course">
     <key column="uid" />
     <many-to-many class="Course" column="uid" />
    </bag>

  </class>

The Person and Course's primary key both named "uid". The hibernate says "Repeated column in mapping".

+2  A: 

Change the name given to the column in the many to many relation.

<bag name="course">
        <key column="uid" />
        <many-to-many class="Course" column="course_uid" />
</bag>

The column in the relation mapping isn't an identifier of the course's column, it is just the name to be used in the relation table.

Zoidberg
Do you mean to change the course's PK to course_uid?
Sefler
nope, leave the course's PK the same. Just change the column name used in the many-to-many relationship. Hibernate will make the course's UID go into the column with that name in the relation table, while automatically relating it to the uid column in the course table.
Zoidberg
A: 

Hi,

You can map your many-to-many mapping as a composite-element as follows

Instead of

<bag name="course">
    <key column="uid"/>
    <many-to-many class="Course" column="course_uid"/>
</bag>

Try

<bag name="course">
    <key column="uid"/>
    <composite-element class="Course">
        // define properties here
    </composite-element>
</bag>

It works regardless of Course mapping file

Arthur Ronald F D Garcia