-->
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: Inserting many to many, generate id for relationship
PostPosted: Tue Mar 10, 2009 12:24 pm 
Newbie

Joined: Thu Feb 26, 2009 7:30 pm
Posts: 7
I want to assign an id (using sequences) to a many-to-many relationship. So if I have two object instances with ID's A_ID and B_ID, I want the relationship table to assign that relationship with an id, R_ID (used as a foreign key in other tables.

Hibernate version: 3.2.4

Mapping documents:
classA.hbm.xml
<hibernate-mapping>
<class name="classA" table="TableA " optimistic-lock="none">
<id name="classAId" type="java.lang.Integer">
<column name="A_ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">MY_SEQ</param>
</generator>
</id>
... (removed properties)
<set name="bees" table="TableAB_Reltn" cascade="all">
<key column="A_ID" not-null="true"/>
<many-to-many column="B_ID" class="ClassB"/>
</set>
</class>
</hibernate-mapping>


classB.hbm.xml
<hibernate-mapping>
<class name="classB" table="TableB">
<id name="classBId" type="java.lang.Integer" unsaved-value="0">
<column name="B_ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">MY_SEQ</param>
</generator>
</id>
... (does not have an inverse set definition)
</hibernate-mapping>

Name and version of the database you are using:
Oracle10g
The generated SQL (show_sql=true):
Hibernate: select HIT_SEQ.nextval from dual
Hibernate: select HIT_SEQ.nextval from dual
Hibernate: select HIT_SEQ.nextval from dual
Hibernate: insert into HIT.HIT_TIER (NAME, TIER_ID) values (?, ?)
Hibernate: insert into HIT.HIT_SUBTIER (NAME, SUBTIER_ID) values (?, ?)
Hibernate: insert into HIT.HIT_SUBTIER (NAME, SUBTIER_ID) values (?, ?)
Hibernate: insert into HIT.HIT_TIER_SUBTIER_RELTN (TIER_ID, SUBTIER_ID) values (?, ?)
Hibernate: insert into HIT.HIT_TIER_SUBTIER_RELTN (TIER_ID, SUBTIER_ID) values (?, ?)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 3:28 pm 
Newbie

Joined: Thu Feb 26, 2009 7:30 pm
Posts: 7
Figured it out. Use <idbag> instead of <set> in the mappings. This allows you to specify a collection id with a generator (sequence in my case). This also means I could use Lists in my java code as opposed to Set's.


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.