-->
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.  [ 8 posts ] 
Author Message
 Post subject: Multiple collections of the same type
PostPosted: Mon May 07, 2007 8:56 pm 
Newbie

Joined: Thu Apr 05, 2007 4:56 pm
Posts: 4
I have 2 classes, A and B:

public A
{
public List<B> list1;
public List<B> list2;
}

public B
{}


Is there a way to implement this through the hibernate mapping files such that:

1) There is only one class B (no empty subclasses solely for discriminator values)
2) There is only one table in the database

I can use the "where" clause in the mapping files, but I don't see anything to specify a discriminator value for a collection upon insert.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 9:01 pm 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
You haven't provied enough detail on what the differences are between list1 and list2.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 9:13 pm 
Newbie

Joined: Thu Apr 05, 2007 4:56 pm
Posts: 4
There isn't any difference... except that they're different lists. They're the same collection structure, same type in the collection, just different instances.

For example:

public PetSanctuary
{
public List<String> catNames;
public List<String> dogNames;
}

except with a user-defined type instead of String.

Edit:

Example hbm file
Code:
<class name="A">
  <id/>
  <bag name="list1">
    <key column="aId"
    <one-to-many class="B"/>
  </bag>
  <bag name="list2">
    <key column="aId"
    <one-to-many class="B"/>
  </bag>
</class>


However: The above does not work because the database does not see the lists as separate, since all they have is a foreign key... so when you repopulate the lists, both lists will have all items from both lists in them. This is easily solved by a discriminator, except that the only way to populate a discriminator value (that I've seen) is to use subclassing, which is not a good thing in this case.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 6:44 am 
Newbie

Joined: Thu May 17, 2007 5:29 am
Posts: 13
Hi Sutherlands,
were you able to solve the problem? I'm having the same issue ...


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 12:01 pm 
Newbie

Joined: Thu Apr 05, 2007 4:56 pm
Posts: 4
Unfortunately not the way I wanted. If you're just looking for a solution try any one of these:

1) Create mapping tables in your database - clean and easy, my recommendation
2) Use subclassing with a where clause in your hbm and a discriminator value - make a list of subA and a list of subB instead of 2 lists of 'class'
3) Put the discriminator value in the POJO - Ugly, and forces a lot of ugly code - the POJO shouldn't know what list it's in
4) Create completely different POJOs - doubles maintainance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 1:41 pm 
Newbie

Joined: Thu May 17, 2007 5:29 am
Posts: 13
What I ended up doing was to create a new class to that "holds" a list, and added getter/setter to my original class that converted from the original List's to two objects and vice-versa
class A{
List<T> a;
List<T> b;
getList_a_AsObj(){
//returns an instance of B<T> containing the list a
}

}
class B<T>{
List<T>a;
}

so now the table corresponding to A has two foreign-keys to a new table corresponding to B, and simple list mapping for the rest....

_________________
--
Saugata
Please rate if this post helps


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 2:52 pm 
Newbie

Joined: Thu Apr 05, 2007 4:56 pm
Posts: 4
In my opinion, having two foreign keys to the same object, where one of them is null, is a bad way to solve the problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 3:41 am 
Newbie

Joined: Thu May 17, 2007 5:29 am
Posts: 13
I think you got me wrong ... (sorry if the code piece wasn't explanatory)

class A contains two instances of B (b1,b2), each containing one of the lists(list1,list2),
the new getter/setter methods (for b1,b2) are responsible for converting between list1,list2 and b1,b2 (and do whatever checks required)

So as far as hibernate is concerned A contains two references to two different instances of B, while B contains one single list of a kind.

_________________
--
Saugata
Please rate if this post helps


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