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.  [ 2 posts ] 
Author Message
 Post subject: Mapping with a composite-id
PostPosted: Tue Feb 05, 2008 8:35 am 
Newbie

Joined: Mon Feb 04, 2008 7:42 am
Posts: 7
I am trying to map a many-to-many relationship where the join table has extra fields. I am trying to follow the hibernate best practises and use two one-to-many associations to an intermediate link class.

The DB setup is:
Hotel: PK=(hotl_id)
Language Data: PK=(hld_hotl_id, hld_lang_id), + extra fields
Language: PK=(lang_id)

Hibernate version: 3.2.5

Mapping documents:

So far I have the following mapping. I would be grateful if someone could help with the mapping I should add to the Hotel.hbm.xml mapping.

LanguageData

Code:
<hibernate-mapping>
   <class name="core.languagedata.LanguageData" table="HTL_LANGUAGE_DATA" lazy="false">
      <composite-id name="id" class="core.languagedata.LanguageDataId">
         <key-property name="hotelId" column="HLD_HOTL_ID"/>
         <key-property name="languageId" column="HLD_LANG_ID"/>
      </composite-id>
      <property name="name" column="HLD_NAME" type="java.lang.String" />
      <property name="description" column="HLD_DESCRIPTION" type="java.lang.String" />
      <!--  many-to-one unidirectional association -->
      <many-to-one name="language" column="HLD_LANG_ID" insert="false" update="false"/>
   </class>
</hibernate-mapping>


Language

Code:
<hibernate-mapping>
   <class name="core.language.Language" table="REF_LANGUAGES">
      <id name="id" column="LANG_ID">
         <generator class="assigned" />
      </id>
      <property name="description" column="LANG_DESCRIPTION" type="java.lang.String" />
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Mapping with a composite-id
PostPosted: Tue Feb 05, 2008 12:16 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
What I personally like to do is to give the relationship table its own primary key and do the mapping as below:

Code:
<hibernate-mapping>
   <class name="core.languagedata.LanguageData" table="HTL_LANGUAGE_DATA" lazy="false">
      <id name="id" column="id"..../>
      <property name="name" column="HLD_NAME" type="java.lang.String" />
      <property name="description" column="HLD_DESCRIPTION" type="java.lang.String" />

      <many-to-one name="language" column="HLD_LANG_ID" />
      <many-to-one name="hotel" column="HLD_HOTL_ID" />
   </class>
</hibernate-mapping>


and for both language and hotel you will have a one-to-many to LanguageData:

Code:

....
<set name="languageData" table="xyz" ...>
     <key column="HLD_LANG_ID"/>
    <one-to-many class="LanguageData" .../>
</set>




However, if you have to keep teh relationship table's schema as it is then you might want to use <key-many-to-one name="propertyName class="ClassName" column="column_name"/>:

Code:
<hibernate-mapping>
   <class name="core.languagedata.LanguageData" table="HTL_LANGUAGE_DATA" lazy="false">
      <composite-id>
         <key-many-to-one name="language" class="Language" column="HLD_LANG_ID"/>       
         <key-many-to-one name="hotel" class="Hotel" column="HLD_HOTL_ID"/>
      </composite-id>
      <property name="name" column="HLD_NAME" type="java.lang.String" />
      <property name="description" column="HLD_DESCRIPTION" type="java.lang.String" />     
   </class>
</hibernate-mapping>


and the many-to-one sides on both Hotel and Language stays the same.


Farzad-


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.