-->
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: Getting distinct results in parent/child w/ List
PostPosted: Wed Jan 18, 2006 2:08 pm 
Regular
Regular

Joined: Mon Nov 14, 2005 7:33 pm
Posts: 73
I'm trying to figure out the best way to handle using a List vs. a Set and if there are real limitiations imposed on me by using a Set for child records in a parent/child relationship.

When using a List I get "duplicates" of the parent record because of the relationship between the two tables as a one-to-many. This can be solved by using a Set but this can be prohibitive in many situations such as:

1) What if I need to dynamically sort the Set? Can this be done? As far I can tell I have to set the order in the mapping xml file which makes the order hard-coded. Otherwise, the results aren't sorted and can change each time the list is refreshed where it is being displayed in the client (e.g. a web browser)

2) If I use a <bag> with Set semantics (Hibernate In Action, Ch. 6, pg. 231) can I have a List that filters out "duplicate" parent results?

3) How can I sort a Set after passing a List into it?
(e.g. Set<Cat> cats = new HashSet(q.list());) - When I do this, the list sorts differently each time I refresh the page, obviously, as it is sorted by a hash code internally and may not remain the same.

I'm looking for a simple, elegant way to do a parent/child relationship w/o losing flexibility (i.e. not able to sort dynamically).

Thanks!

Hibernate version: 3.1

Name and version of the database you are using: MSSQL 2000


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 5:08 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you want to dynamically sort a set, create a List from it and pass it to Collections.sort, or create a TreeSet from it with the correct Comparator. There are many other alternatives, too. If you use a SortedSet in your mapping file, the order is fixed, but can be changed in another Set.

Alternatively, use a Set (unsorted) and make use of order by in your HQL query. Hibernate backs Sets with PersistentSet, not HashSet, so doesn't necessarily sort by hashCode. If you use Criteria, use addOrder.

Re: point 3: why would the hash code change each time you load a set or list? Are you using java's default hashCode/equals for persistent objects? This is a very bad idea, seeing as you're using sets/lists: you should use a business key (not a DB key) for this purpose. Read section "Implementing equals() and hashCode()" in chapter "Persistent Classes" of the hib ref docs.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 5:34 pm 
Regular
Regular

Joined: Mon Nov 14, 2005 7:33 pm
Posts: 73
Actually, I think I was able to solve all of my problems here w/ #2 in my original post.

I configured the List as a <bag> but w/ Set semantics...and the parent records in the list are unique. Also, as it is a List, it can be sorted simply by using an order by in HQL or Order in a Criteria query.

What had happened is...I was pulling data 3-join levels deep and on the last tier of the join I was using "join" in the collection mapping instead of "select"...once I switched it...the results were unique again.

So, basically, I learned a little more about the dimensions of how queries are handled in the object graph...it makes sense to me now.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 5:45 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Cool. BTW, you can sort <set>s in HQL and with Criteria, just like <bag>s and <list>s. The only substantive different between bags and sets is that bags allow multiple instances of the same object: they're good for tables with no keys.


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.