-->
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: Mapping when a single column is used in two foreign Keys
PostPosted: Mon Jan 24, 2005 12:31 pm 
Newbie

Joined: Mon Jan 24, 2005 10:22 am
Posts: 2
Location: Middleburgh, NY
Suppose I have something like this:

Table T1 with primary key (common_id, T1_ID)

Table T2 with primary key (common_id, T2_ID)

and an association table A3 modeling a many-to-many relation between
T1 and T2. A3 has primary key (common_id, T1_ID, T2_ID), and
two foreign keys: (common_id, T1_ID) =>T1, and
(common_id, T2_ID) =>T2. Note that both foreign keys
share the "common_id" column.

When I try to map this in hibernate, I try to construct
the class for A3 using a composite-id something like this:

<composite-id>

<key-many-to-one name="T1">
<column name="common_id"/>
<column name="T1_ID"/>
</key-many-to-one>

<key-many-to-one name="T2">
<column name="common_id"/>
<column name="21_ID"/>
</key-many-to-one>

</composite-id>


However, when I try to use a mapping like the above, I get an error about common_id
being a repeated column, and the message suggests that I
use update="false" and insert="false" in defining the property. That doesn't
really make sense for a composite-id, though.

So, is there actually a way to map these three tables into three classes? (other
than some kind of crazy hack, like defining a view that gives two different names
to the same column or something like that.)

Thanks for shedding any light.

_________________
Bruce Cota


Top
 Profile  
 
 Post subject: Did you solved it?
PostPosted: Tue Feb 01, 2005 5:12 am 
Newbie

Joined: Tue Feb 01, 2005 5:04 am
Posts: 8
Location: Barcelona
I've the same problem the key of my new object is a reference to two objects wich have a composite-id having a repeated column.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 01, 2005 5:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
To begin with, I don't think <key-many-to-one> is really that nice a feature. So, first, get rid of that ;-)

Create a pk class, FooBarKey, with the 3 pk attributes:

Code:
<class name="FooBar">

  <composite-id name="id" class="FooBarKey">
     <key-property name="foo_id"/>
     <key-property name="bar_id"/>
     <key-property name="common_id"/>
  </composite-id>

  <many-to-one name="foo" insert="false" update="false">
     <column name="foo_id"/>
     <column name="common_id"/>
  </many-to-one>

  <many-to-one name="bar" insert="false" update="false">
     <column name="bar_id"/>
     <column name="common_id"/>
  </many-to-one>

   ....

</class>


We should add something like this to the wiki or doco or faq...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 01, 2005 8:52 am 
Newbie

Joined: Tue Feb 01, 2005 5:04 am
Posts: 8
Location: Barcelona
Yes I've bypassed the problem in a similar way. Leaving only one object in the key and a property. And the other object in the class.

But from the objects view it's not the best solution I've to duplicate code. Will this be improved in the version 3? It will be an interesting feature.

Thanks


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.