Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Many-to-many hibernate xml configuration

I have such DB relationship.
db scheme

I want to have many-to-many relationship. Between PLAYER and PRIVILEGE. Could you please help me to fix my .xml configs.

Expected result: I want to be able to execute: String hql = "from Player as p right outer join p.privilages as priv";

Actual: So far I get:

org.hibernate.MappingException: Foreign key (FK8CD18EE134F64423:PLAYER [ID])) must have same number of columns as the referenced primary key (PRIVILAGE [ID,PRIVILAGE])

<hibernate-mapping>
    <class name="model.Privilage" table="PRIVILAGE">
        <id name="id" type="int" >
            <column name="ID"  precision="5" scale="0"/>
            <generator class="increment"/>
        </id>

        <set name="players" table="PLAYER"
             inverse="false" lazy="true" fetch="select" cascade="all" >
            <key>
                <column name="ID"/>
            </key>
            <many-to-many entity-name="model.Player">
                <column name="ID" not-null="true" />
            </many-to-many>
        </set>

        <property name="privilage" type="string">
            <column name="PRIVILAGE" length="20" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

and

<class name="model.Player" table="PLAYER">
    <id name="playerId" type="int" >
        <column name="ID" precision="5" scale="0"/>
        <generator class="sequence">
            <param name="sequence">PLAYER_SEQ</param>
        </generator>
    </id>
    <set name="privilages" table="PRIVILAGE"
         inverse="false" lazy="true" fetch="select" cascade="all" >
        <key>
            <column name="ID"/>
        </key>
        <many-to-many entity-name="model.Privilage">
            <column name="PRIVILAGE" not-null="true" />
        </many-to-many>
    </set>
    <!-- ... -->
</class>

like image 527
Rudziankoŭ Avatar asked Dec 30 '15 16:12

Rudziankoŭ


1 Answers

You should reference your many-to-many relationship table called PLAYER_PRIV:

<set name="privilages" table="PLAYER_PRIV"
     inverse="true" lazy="true" fetch="select">
    <key>
        <column name="ID"/>
    </key>
    <many-to-many entity-name="model.Privilage">
        <column name="PRIV_ID" not-null="true"/>
    </many-to-many>
</set>

and

<set name="players" table="PLAYER_PRIV"
     inverse="false" lazy="true" fetch="select">
    <key>
        <column name="ID"/>
    </key>
    <many-to-many entity-name="model.Player">
        <column name="PLAYER_ID" not-null="true"/>
    </many-to-many>
</set>
like image 113
Mosolova Avatar answered Nov 08 '22 15:11

Mosolova