-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Example of unidiretional many-to-many with composite keys
PostPosted: Thu Apr 12, 2007 4:17 pm 
Newbie

Joined: Wed Feb 07, 2007 6:05 pm
Posts: 10
Can anyone point me to an example using a unidirectional many-to-many relationship with both entities have composite primary keys.

Entity A has a primary key that looks like this

int id
String name
String myName

Entity B has a primary key that looks like this

int id
String name
String anotherName

I want to define the many-to-many in A's mapping file.

every possible combination I have attempted always generates the mapping exception

A must have same number of columns as the referenced primary key B

any help would be greatly appreciated.

(I have read through the java persistence with hibernate, but they always use simple keys).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 12:37 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Assuming that id and name are in the join table (hopefully you can figure out any other permutations from this example):
Code:
<class name="B" ...>
  <composite-id>
    <key-property name="id" type="integer"/>
    <key-property name="name" type="string"/>
    <key-property name="anotherName" type="string"/>
  </composite-id>
  <properties name="joinRefB">
    <property name="id" insert="false" update="false"/>
    <property name="name" insert="false" update="false"/>
  </properties>
  ...
</class>
Code:
<class name="A" ...>
  <composite-id>
    <key-property name="id" type="integer"/>
    <key-property name="name" type="string"/>
    <key-property name="myName" type="string"/>
  </composite-id>
  ...
  <set name="Bs" table="AtoBlink" ...>
    <key>
      <formula>id</formula>
      <formula>name</formula>
    </key>
    <many-to-many class="A" property-ref="joinRefB"/>
  </set>
</class>
I think.

Maybe :)

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 10:54 am 
Newbie

Joined: Wed Feb 07, 2007 6:05 pm
Posts: 10
Thanks for the input.

The XML for class A

Code:
    <key>
      <formula>id</formula>
      <formula>name</formula>
    </key>


is generating this error

The content of element type "key" must match "column"

Should the formula tags be column tags?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 11:42 am 
Newbie

Joined: Wed Feb 07, 2007 6:05 pm
Posts: 10
Ok, this is what I ended up with and it works fine.

This is real entity that represents B
Code:
<hibernate-mapping default-lazy="false">
   <class name="CifsShare" table="CifsShare">
      <composite-id name="key" class="CifsSharePk">
         <key-property name="dataStoreId" />
         <key-property name="primaryIpAddr"/>
         <key-property name="shareName"/>
      </composite-id>

       <property name="fileSystem"/>
       <property name="relativePath"/>
       <property name="description"/>

        <properties name="joinRefCifsServer">
           <property name="shareName" insert="false" update="false"/>
        </properties>
    </class>
</hibernate-mapping>


This is real entity that represents A
Code:
<hibernate-mapping default-lazy="false" >
   <class name="CifsFileServer" table="CifsFileServer">
      <composite-id name="key" class="CifsFileServerPk">
          <key-property name="dataStoreId" />
          <key-property name="primaryIpAddr"/>
          <key-property name="cifsIpAddr"/>
      </composite-id>

      <property name="adJoinDomain"/>
      <property name="adNetbiosName"/>
      <property name="netbiosName"/>

      <set name="cifsShares" table="CifsServerShares" cascade="save-update">
         <key>
            <column name="dataStoreId"/>
            <column name="primaryIpAddr"/>
            <column name="cifsIpAddr"/>
          </key>
          <many-to-many class="CifsShare" property-ref="joinRefCifsServer"/>
      </set>
   </class>
</hibernate-mapping>


Thanks for your help!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.