-->
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.  [ 5 posts ] 
Author Message
 Post subject: Composite ids between many tables
PostPosted: Mon Sep 13, 2010 1:34 pm 
Newbie

Joined: Mon Sep 13, 2010 1:08 pm
Posts: 5
Hi, I'm pretty new with Hibernate, and I'm facing some trouble working with composite ids and foreing keys,
I just want to make this work:

Code:
=======    =======
Table A    Table B
=======    =======
atr1 PK    atr1 FK(Table A, atr1) PK
atr2 PK    atr2 FK(Table A, atr2) PK
atr3       atr3 FK(Table C, atrN) PK
           atr4


And the relation is the following: A has (knows) none ore more instances of B (a list of them), and B has (knows) just one instance of C (this is not a problem by now).

I've read something about using classes to point composite ids (in the official tutorial), overriding equals() and hashCode() but I can't make this work because I'm pretty confused about how to write my hbm.xml files...

Any help? Thanks!

_________________
Joaquin L. Robles
reweb
www.reweb.com.ar


Top
 Profile  
 
 Post subject: Re: Composite ids between many tables
PostPosted: Mon Sep 13, 2010 7:43 pm 
Newbie

Joined: Sun Jun 20, 2010 9:57 pm
Posts: 7
Location: Madison WI
I think you can do this by creating a normal hbm mapping corresponding to Table A, something like the following, assuming all of the columns in the tables are string-valued:

Code:
<hibernate-mapping>
  <class name="A" table="A">
    <composite-id>
      <key-property name="atr1" type="string" column-name="atr1">
      <key-property name="atr2" type="string" column-name="atr2">
      <key-property name="atr3" type="string" column-name="atr3">
    </composite-id>
  </class>
</hibernate-mapping>


Then for Table B, you could use an hbm mapping like the following:

Code:
<hibernate-mapping>
  <class name="B" table="B">
    <composite-id name="key" class="A">
      <key-property name="atr1" type="string" column-name="atr1">
      <key-property name="atr2" type="string" column-name="atr2">
      <key-property name="atr3" type="string" column-name="atr3">
    </composite-id>
    <property name="atr4" type="string" column-name="atr4"/>
  </class>
</hibernate-mapping>


Sarah


Top
 Profile  
 
 Post subject: Re: Composite ids between many tables
PostPosted: Mon Sep 13, 2010 8:00 pm 
Newbie

Joined: Sun Jun 20, 2010 9:57 pm
Posts: 7
Location: Madison WI
Wait . . . why is atr3 part of the primary key in Table B if it's not part of the primary key in Table A? There's little reason to carry it along into Table B and it breaks some fundamental database design principles. If you don't need it, then you could use the following mappings:

Class A_PK just defines atr1 and atr2 of Table A.
Class A represents all of Table A.
Class B represents all of Table B.

Code:
<hibernate-mapping>
  <class name="A" table="A">
    <composite-id name="key" class="A_PK">
      <key-property name="atr1" type="string" column-name="atr1"/>
      <key-property name="atr2" type="string" column-name="atr2"/>
    </composite-id>
    <property name="atr3" type="string" column-name="atr3"/>
  </class>
</hibernate-mapping>

<hibernate-mapping>
  <class name="B" table="B">
    <composite-id name="key" class="A_PK">
      <key-property name="atr1" type="string" column-name="atr1"/>
      <key-property name="atr2" type="string" column-name="atr2"/>
    </composite-id>
    <property name="atr4" type="string" column-name="atr4"/>
  </class>
</hibernate-mapping>


If you do need atr3 to remain in Table B, then I'm not sure you can do much else other than maintain it manually as another <property> in the mapping for Table B. I'd be curious to know if there's a way to do it.

Sarah


Top
 Profile  
 
 Post subject: Re: Composite ids between many tables
PostPosted: Mon Sep 13, 2010 8:54 pm 
Newbie

Joined: Mon Sep 13, 2010 1:08 pm
Posts: 5
Sarah,
Thanks for the response, I'll try it...
atr3 hasn't the same semantics in tables A and B, it could have a totally different meaning... I was just enumerating the attributes in a generic way, maybe I should have used another naming strategy...

_________________
Joaquin L. Robles
reweb
www.reweb.com.ar


Top
 Profile  
 
 Post subject: Re: Composite ids between many tables
PostPosted: Tue Sep 14, 2010 4:41 pm 
Newbie

Joined: Sun Jun 20, 2010 9:57 pm
Posts: 7
Location: Madison WI
I took a look at your requirements more carefully - Table B is a join table between Table A and Table C. I misunderstood that initially. Given that Table A has a composite key, I'm stumped as to how to do it.

I think you want a mapping for Table A as follows that handles the composite key and the non-key column:

Code:
<hibernate-mapping>
  <class name="A" table="A">
    <composite-id>
      <key-property name="atr1" type="string" column-name="atr1">
      <key-property name="atr2" type="string" column-name="atr2">
    </composite-id>
    <property name="atr3" type="string" column-name="atr3">
  </class>
</hibernate-mapping>


The mapping for Table C is straightforward since it has a non-composite key - you can just use an <id> tag for the primary key column.

The question becomes how to do the mapping for Table B. I was thinking you'd do a <composite-id> with a <key-many-to-one> to map the Table A foreign key columns and another <key-many-to-one> to map the Table C foreign key columns. It looks like the <key-many-to-one> syntax allows you to specify multiple nested <column> tags, but I'm not sure how you map the columns into the distinct Java properties.

Sorry to send you down the wrong path with my original response. I misunderstood the table structures.

Sarah


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