-->
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: One to Many Mapping
PostPosted: Mon Apr 12, 2004 7:05 pm 
Newbie

Joined: Thu Apr 01, 2004 2:19 pm
Posts: 14
I have 3 classes:

Class A {
private long id;
}

Class B{
private long id;
}

Class C{
private long id;
private long ref_jd;
private String type;
}

Both A and B share one-to-many relationship with C. C stores id of A or B and in ref_id and the String type identifies whether this is related to A or B.

If I define A.hmb.xml as below:

<bag name="cList" lazy="false" cascade="all">
<key column="ref_id"/>
<one-to-many class="C"/>
</bag>

It does not take care of the type column...Is there another way to define this so that I can define a composite key.

Please help....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 1:19 am 
Regular
Regular

Joined: Mon Oct 20, 2003 3:14 am
Posts: 53
Location: Sterling, VA, USA
There is, involving the hashing of composite keys that you need to do to have them, but you don't want to do go this route if you can avoid it. Composite keys have all sorts of warts, in Hibernate, and life. The least of them are, "What happens if the structure of your composite key changes??"

Do this:

Declare an "artificial key" for the table -- say an autonumbering (identity/native) primary key. Use this in hibernate as the id.

If it makes you feel better to have a "real key" defined, set unique constraint information on the business key field's you would normallly use (this is what I do).

I'm not quite the militant reciter of the "database keys should not be the business keys" mantra you normally find around here. If you have a lookup table with one field in it, then yeah, don't worry about making an artificial key for it.

HTH

-Joe

_________________
"A statistician is a mathmetician, broken down by age and sex".


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 10:26 am 
Newbie

Joined: Thu Apr 01, 2004 2:19 pm
Posts: 14
Class C already has its own id. The problem is A and B both reference to C. So Data in C could look like:

ID REF_ID TYPE
1 1 A
2 2 A
3 1 B
4 2 B
5 3 B


So if I say that REF_ID is the only key, it could load either the object with type A or B. How do I avoid this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 11:38 pm 
Regular
Regular

Joined: Mon Oct 20, 2003 3:14 am
Posts: 53
Location: Sterling, VA, USA
I'm really wondering about the design here.

Youre essentually trying to declare a one-many to different objects using the same field.

Why can't What exactly is it youre trying to do?

IMHO, I'd have some common superclass for A and B, and have a one-many with it.

I haven't tried this myself but I'm thinking Hibernate figure out which class youre talking about when you query on C.

_________________
"A statistician is a mathmetician, broken down by age and sex".


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.