-->
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 2 tables with a look-up tabe
PostPosted: Mon Oct 19, 2009 6:13 pm 
Newbie

Joined: Mon Oct 19, 2009 5:44 pm
Posts: 6
I'm sure that this has come up in the past so sorry in advance if that's the case. I did find one thread but it got off track and never addressed the initial problem.

I have two tables for ease of use we'll just call them Parent and Child they have a many-to-many relationship so I add a look-up table(at least that's what I call them) called Parent_Child_Lookup that only has two fields parent_id and child_id this table only exists to link the parents with their children.

My Java objects are named the same as the table names.

Parent.java
- parentID int
- parentName String
- children LinkedList<Child>

Child.java
- childID int
- childName String

I cant figure out for the life of me a way to map these tables to show the relationship between the two. It would be great to know if there is a way to do this without having to create an object for the lookup table. Does this make sense? Any help or advice would be great. Thanks,
Jon


Top
 Profile  
 
 Post subject: Re: Mapping 2 tables with a look-up tabe
PostPosted: Mon Oct 19, 2009 6:27 pm 
Newbie

Joined: Mon Oct 19, 2009 4:32 pm
Posts: 3
Well I don't know if I do properly answer but if you add a getter and setter for "parents" in your child.java and that in your hibernate mapping file for your child class:
<set name="parents" table="parentchild">
<key column="childID"/>
<many-to-many class="domain.Parent" column="parentID"/>
</set>
+ do the opposite for the Parent:
add a getter and setter for "children" and that in the hbm.xml file:
<set name="children" table="parentchild">
<key column="parentID"/>
<many-to-many class="domain.Child" column="childID"/>
</set>
, you don't have to create a specific java class for your junction table.


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.