-->
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: How to map many-to-many relations with additional properties
PostPosted: Mon Jun 19, 2006 7:55 am 
Newbie

Joined: Thu Jun 01, 2006 8:17 am
Posts: 10
Location: mostly lake of constance
I want to map a many-to-many relation, which is saved in a relation. the problem is, that this relation has additional properties, so i can´t use the many-to-many syntax i guess. but i have no clue how to map that. anybody a good idea?

gru brs[/img]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 12:48 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You can do this using composite elements and many-to-one (refdocs 8.2 and 23.3). Or you can map the join table as a separate entity (refdocs 7.5.1, one-to-many/many-to-one). Or, if your join table has only a single additional property, being a sequence, then you can use a list with many-to-many. This situation crops up quite a lot, and it's very handy that hibernate handles it so cleanly.

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


Top
 Profile  
 
 Post subject: well, just one little problem yet ;-)
PostPosted: Tue Jun 20, 2006 5:36 am 
Newbie

Joined: Thu Jun 01, 2006 8:17 am
Posts: 10
Location: mostly lake of constance
my mapping file looks now as the following:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="orm">
   <class name="ObjectItem" table="OBJ_ITEM">
      <meta attribute="class-description">Represents an Object Item</meta>
      <id name="id" column="obj_item_id" type="java.lang.Long">
         <generator class="native"/>
      </id>
      <property name="categoryCode" column="cat_code" type="string" length="6" not-null="true"/>
      <property name="name" column="name" type="string" length="100" not-null="true"/>
      <property name="alternateIdentificationText" column="altn_identific_txt" type="string" length="255"/>
      <property name="ownerId" column="owner_id" type="long" not-null="true"/>
      <property name="updateSeqNo" column="update_seqnr" type="long" not-null="true"/>
      <list name="capabilities" table="OBJ_ITEM_CAPAB" cascade="all">
         <meta attribute="use-in-tostring">true</meta>
         <key column="obj_item_id"/>         
         <index column="obj_item_capab_ix" />
         <composite-element class="ObjectItemCapability">
            <many-to-one name="capability" class="Capability" cascade="all">
               <meta attribute="use-in-tostring">true</meta>
               <column name="capab_id"/>
            </many-to-one>            
            <property name="missionPrimacyCode" column="msn_primacy_code" type="string" length="6"/>
            <property name="reportingDataId" column="rptd_id" type="long"/>
            <property name="ownerId" column="owner_id" type="long" not-null="true"/>
            <property name="updateSeqNo" column="update_seqnr" type="long" not-null="true"/>
         </composite-element>
      </list>
      <list name="locations" table="OBJ_ITEM_LOC" cascade="all">
         <meta attribute="use-in-tostring">true</meta>
         <key column="obj_item_id"/>         
         <index column="obj_item_loc_ix" />
         <composite-element class="ObjectItemLocation">      
            <many-to-one name="location" column="loc_id" class="Location" cascade="all">
               <meta attribute="use-in-tostring">true</meta>
            </many-to-one>            
            <property name="accuracyQuantity" column="acc_qty" type="double" not-null="false"/>
            <property name="bearingAngle" column="brng_angle" type="double" not-null="false"/>
            <property name="bearingAccuracyAngle" column="brng_acc_angle" type="double" not-null="false"/>
            <property name="speedRate" column="speed_rate" type="double" not-null="false"/>
            <property name="speedAccuracyRate" column="speed_acc_rate" type="double" not-null="false"/>         
            <property name="useCatCode" column="use_cat_code" type="string" length="6" not-null="true"/>
            <property name="reportingDataId" column="rptd_id" type="long"/>
            <property name="ownerId" column="owner_id" type="long" not-null="true"/>
            <property name="updateSeqNo" column="update_seqnr" type="long" not-null="true"/>
         </composite-element>
      </list>
   </class>
</hibernate-mapping>


the only wish i have, is that the obj_item_capab_ix and the obj_item_loc_ix will be generated using the native generator. how to do that? i tried it somehow, but it doesn´t work!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 5:37 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Those two columns, being defined as index (same as list-index, as I found out two days ago), will be the index of the composite element within the list. If you use base="1", 1 will be added to the java list's index in order to come up with the database id value. I think that's pretty much what the native generator does, isn't it? The only thing that's potentially different is if the native generator is falling back to identity, then it won't work, because identity means that the number is chosen by the DBMS, whereas index is assigned by java/hibernate.

The easy solution (assuming that the obj_item_capab_ix and obj_item_loc_ix columns are currently sequences/identities) is to make those columns simple ints, and drop the DB sequence/identity requirement. Hibernate will implicitly guarantee the same effect for you, because (obviously) you can't have two items at index 1 in a list. The downside is that a particular row might have id 2 when it's loaded, but if row 1 is deleted, row 2 will be saved as row 1 afterwards. But hibernate looks after all of that for you, so usually you don't have to worry about it.

If that's not suitable for you, I believe that you'll have to switch to using the one-to-many/many-to-one style mapping (refdocs section 8.2), in which ObjectItemCapability and ObjectItemLocation are mapped as their own classes, rather than as composite elements.

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


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.