-->
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.  [ 3 posts ] 
Author Message
 Post subject: How best to implement containment of a class in many places
PostPosted: Tue Dec 11, 2007 9:49 pm 
Newbie

Joined: Sat Jan 20, 2007 10:20 pm
Posts: 6
If you have classes A, B and C, all three not related comment parent or by inheritance. You also have a fourth class called X, again not related to the previous three. Your object model has the following one-to-many relationships
A---on-to-many--->X
B---on-to-many--->X
C---on-to-many--->X
The question is: How best to implement all three relationships between A and X, B and X, C and X using Hibernate? There are some well-known solutions in standard object-modeling theory, like using a joint-table, but I would like to get an answer from the makers of Hibernate to see if they created something to handle this particular case. As far as I know, this topic is not mentioned in the “Java Persistence with Hibernate” book.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 12, 2007 9:34 am 
Beginner
Beginner

Joined: Tue Apr 24, 2007 12:53 pm
Posts: 28
Quote:
If you have classes A, B and C, all three not related comment parent or by inheritance. You also have a fourth class called X, again not related to the previous three. Your object model has the following one-to-many relationships
A---on-to-many--->X
B---on-to-many--->X
C---on-to-many--->X
The question is: How best to implement all three relationships between A and X, B and X, C and X using Hibernate? There are some well-known solutions in standard object-modeling theory, like using a joint-table, but I would like to get an answer from the makers of Hibernate to see if they created something to handle this particular case. As far as I know, this topic is not mentioned in the “Java Persistence with Hibernate” book.



I am not so sure that Hibernate would need anything special to handle this case. Its a matter of Database design & theory. If there is no commonality between A, B, and C (ie: they each represent their own table) Then the only way to reference X by any of them is through a Join-Table or via ForeignKey relationship. In these cases, Hibernate already fully supports either mechanism, going both directions along the relationship.

IMHO: You shouldn't model your relationships of your data model based on what Hibernate (or any tool for that matter) supports. Instead model it based on the true relationship, and then figure out how to support that realization using whatever tool you've decided on (Hibernate in this case).

-B


Top
 Profile  
 
 Post subject: Re: How best to implement containment of a class in many pla
PostPosted: Thu Dec 20, 2007 12:35 am 
Newbie

Joined: Mon Nov 13, 2006 8:01 am
Posts: 2
colbert_philippe wrote:
If you have classes A, B and C, all three not related comment parent or by inheritance. You also have a fourth class called X, again not related to the previous three. Your object model has the following one-to-many relationships
A---on-to-many--->X
B---on-to-many--->X
C---on-to-many--->X
The question is: How best to implement all three relationships between A and X, B and X, C and X using Hibernate? There are some well-known solutions in standard object-modeling theory, like using a joint-table, but I would like to get an answer from the makers of Hibernate to see if they created something to handle this particular case. As far as I know, this topic is not mentioned in the “Java Persistence with Hibernate” book.



Join table is a solution, but if you prefer foreign keys you could use this solution:

<class name="A">
<set name="bb1" cascade="all" sort="natural">
<key column="a1"/>
<one-to-many class="B"/>
</set>

<set name="bb2" cascade="all" sort="natural">
<key column="a2"/>
<one-to-many class="B"/>
</set>
</class>

<class name"B">
<many-to-one name="a" class="A" fetch="join">
<formula>case when a1 is not null then a1 when a2 is not null then a2 end</formula>
</many-to-one>
</class>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.