-->
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 one-to-many using multi-fields as FK
PostPosted: Tue Apr 01, 2008 7:27 am 
Newbie

Joined: Tue Apr 01, 2008 6:32 am
Posts: 2
I wonder how to map one-to-many with multi-fields as Foreign Key.
Suppose there are two tables:
Table1(
T1F1 NUMBER PRIMARY KEY,
T1F2 NUMBER

)

Table2(
T2F1 NUMBER PRIMARY KEY,
T2F2 NUMBER,
T2F3 NUMBER

)

For Table1, I don't want to use composite key as the primary key.
Although T1F2 is not the the PK of Table1, I need to use it as one of Table2's FKs.Table2' FK is composed of T2F2 and T2F3,which references T1F1 and T1F2 in Table1,respectively.
Based on this assumption, how can I map a one-to-many from Table1 to Table2?
Based on below code, how should the mapping be?
class Class1{
//....
private Set<Class2> parts;

}

Class1 will be mapped to Table1,
while Class1.parts will be mapped Table2,
there is an one-to-many relationship, but how should I setup the mapping?
Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 1:11 am 
Newbie

Joined: Sun Apr 20, 2008 8:11 pm
Posts: 10
If someone has a solution for this problem, I am also stuck because of this...

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 3:01 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
I don't want to use composite key as the primary key.


You know, that just might be a potential solution.

Compound primary keys aren't that difficult to implement or manage. And there are many ways to implement compound primary keys, from simply using the @Embeddable notation, to the @IdClass.

Here's a little tutorial on the subject of compound primary keys with Hibernate3 and JPA annotations. Give it a whirl and see if it can't help to solve your problem.



http://www.thebookonhibernate.com/HiberBookWeb/learn.jsp?tutorial=15usingcompoundprimarykeys

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 19, 2008 6:04 am 
Newbie

Joined: Tue Apr 01, 2008 6:32 am
Posts: 2
Thanks Cameron McKenzie for your information.
There is one thing noticeable: it is harder to load an object with a compound PK, that's why I didn't want use a compound key.


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.