-->
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.  [ 2 posts ] 
Author Message
 Post subject: Multiple collections of same type
PostPosted: Mon Feb 01, 2010 4:13 pm 
Newbie

Joined: Mon Mar 08, 2004 12:32 pm
Posts: 7
Is there a canonical way of dealing with a parent entity that has multiple collections of the same child entity?

e.g.
Code:
class Aficionado {
  private List<Car> ownedCars;
  private List<Car> rentedCars;
}


I've tried using a property (backed by an enum in the Car class) to sort them, but this is failing because both collections are trying to add a duplicate AFICIONADO_ID column to the CAR table.

Code:
<!-- Aficionado.hbm.xml -->
<list name="ownedCars" inverse="false" batch-size="20" cascade="save-update,delete,delete-orphan"
  where="CAR_TYPE='owned'">
  <key column="AFICIONADO_ID" not-null="true" />
  <list-index column="ORDER_INDEX" />
  <one-to-many class="com. ... .Car" />
</list>

<list name="rentedCars" inverse="false" batch-size="20" cascade="save-update,delete,delete-orphan"
  where="CAR_TYPE='rented'">
  <key column="AFICIONADO_ID" not-null="true" />
  <list-index column="ORDER_INDEX" />
  <one-to-many class="com. ... .Car" />
</list>


Is there a canonical way to go about this? Is the best way just to suck it up and have two foreign key columns in CAR?


Top
 Profile  
 
 Post subject: Re: Multiple collections of same type
PostPosted: Fri Feb 12, 2010 3:50 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi,

as you did not report how you mapped class Car,
it is not completely clear to me whether you use/want bidirectional or unidirectional relationship.

Quote:
both collections are trying to add a duplicate AFICIONADO_ID column to the CAR table


This suggests me that you want to have the relationship bidirectional.
Well in this case, that what you intend to do currently is not mappable as it goes against Uml-specification,
you are currently trying to do something like this

+----------------+ +---------------------+
| AFICIO |---\ | CAR |
| | >------------- | af_id |
| |---/ | |
+----------------+ +---------------------+

But you cannot start from Aficio with 2 relations and arrive to Car just with one.
You cannot 'share' the same field CAR.AFICIONADO_ID for 2 different relations!


Solution:

Alternative1: model the 2 relations unidirectional from Aficio to Car (specifying 2 different secondary table names !)

+----------------+ +---------------------+
| AFICIO |------------------->| |
| | | CAR |
| |-------------------->| |
+----------------+ +---------------------+

Alternative2: use different key column names in order that Car has 2 different foreign keys,
in this case you can remove the where-clause

+----------------+ +---------------------+
| AFICIO |---------------------| af_id_rent |
| | | CAR |
| |--------------------- | af_id_own |
+----------------+ +---------------------+


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