I have such DB relationship.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With