• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate multiple one-to-many relationships to the same class

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have multiple one-to-many relationships to the same class from a class. Is there a way to Map it?

<class name="A" table="A">
...
<set name="x">
<key column="a"/>
<one-to-many class="B"/>
</set>

<set name="y">
<key column="a"/>
<one-to-many class="B"/>
</set>

<set name="z">
<key column="a"/>
<one-to-many class="B"/>
</set>
</class>
--------------------------------------------
class B {
public String value;
}
--------------------------------------------
I just use B to save a list of options for different attributes of A. Can I map using one generic class B or do I need to have a different class for each attribute in A (Bx, By, Bz). Is there a better way to address the whole issue?

Thanks in advance
Ram
[ March 14, 2006: Message edited by: Ram Raju ]
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mapping it as you have done is OK. You need three different properties, each a Set of the same class. Hibernate will only load a class B one in a session, no matter how many sets it appears in.
 
Ram Raju
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

Thankyou for the response. I have a problem with the mapping I posted. All the entries in table B for a particular A are loaded into all the sets (x, y, z). I guess this is because there is nothing in the mapping to diffrentiate b/w various sets.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I guess this is because there is nothing in the mapping to diffrentiate b/w various sets.


No, there is something to differentate between the sets - each has a different name. However, each have the same key. So you have three sets, all describing the same relationship.
 
Ram Raju
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

I am not sure if I described my problem correctly. I want the values in x, y, z to be different.

x -> "alpha", "beta"
y -> "gamma"

but with current mapping, all values are getting loaded into all the attributes.

x -> "alpha", "beta", "gamma"
y -> "alpha", "beta", "gamma"

Since the database structure for table B is

------------------
id | value | a_id
------------------
1 | alpha | 1
2 | beta | 1
3 | gamma | 1

How would it know what to load in x and what to load in y.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate is loading the data just as you have defined it. a_id == 1, so a valid set of table B objects for any of your mapped sets is {alpha, beta, gamma}. The way your data model is defined the only way you will restrict the set is if a_id is different for each set. And that can't happen for the same record, since a_id is an FK to the PK of table A.
 
Ram Raju
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you. So can you please suggest on how to implement this.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think we are talking at cross purposes.

Given your data model, you have one set of table B objects per table A, and there is no way round that. You can't use Hibernate to subvert the constraints of the relational model. I think you problem is you think you've got a one-to-many relationship and you really got a many-to-many relationship. How do you currently get this data via SQL?
 
Ram Raju
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well this is new development. Data model changes are still welcome.

my goal -> [B]
------------------------------------
id | value | type | a_id
------------------------------------
1 | alpha | x | 1
2 | beta | x | 1
3 | gamma | y | 1

and hibernate should take care of populating the object x/y based on the type. I need to know if I can do this and how to write the mapping for that.

Thanks
Ram
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah now we've got a polymorphic mapping, which make a little more sense. In theory, it would be possible to have a set of sub classes of "TableB" which Hibernate could create based on a discriminator. However, the FK constraint would still cause all table B records to be returned for all your table A sets. Its just that this time you'd have more than on type of object in your set. So, as your data model stands you still can't get what you are after.

Suppose you have your set of values {alpha, beta, gamma} contained in one table and your tableA properties in another, to implement the relationship you want you'll need a third table to implement the many-to-many relationship. So:



This would give you your many to many.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
The relation from A is set as a foreign key field in B.
So you must define in B to which relation in A you are referring
=> composite-id
B
(type, a_id, other field)

Have a look in the Hibernate reference (or in my book) and search for composite-id and formula. There is an example.
Best Regards Sebastian
 
Ram Raju
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou for your help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic