-->
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 an ordered collection with dups/gaps in order col
PostPosted: Wed Feb 17, 2010 7:23 pm 
Newbie

Joined: Fri Feb 05, 2010 3:28 pm
Posts: 5
Hello Hibernators

I have two tables, VENDOR_SIZE_CHART and VENDOR_SIZE_VALUE. Each VENDOR_SIZE_CHART is associated with many VENDOR_SIZE_VALUES.

VENDOR_SIZE_VALUE has a SORT_ORDER column, I would like Hibernate to use this column to order the collection every time its retrieved. This column can have gaps (i.e. 1,2,3,<gap>,5,6,7) and duplicates. I don't really care about tie breaking order in case of duplicates, but there is a primary key ID column to use.

The end user can update SORT_ORDER through the web application, if that happens the next time VENDOR_SIZE_CHART is updated the VALUE's should be retrieved in the new order.

I'm having trouble implementing this in HBM. Here is my mapping:

Code:
      <list
            name="vendorSizeValues"
            inverse="true"
            cascade="all"
            >
         <key column="VENDOR_SIZE_CHART_ID" not-null="true"/>
         <list-index column="SORT_ORDER" base="1"/>
         <one-to-many class="com.bluefly.apps.manager.domain.VendorSizeValue" />
      </list>

I'm having the following problems:
- I get an ArrayIndexOutOfBounds error when there are duplicate sort order values
- null elements are being placed in the list where the gaps occur, as if the collection was an fixed length array indexed by sort_order

Is there a way to tell Hibernate to not place null elements in the list, and to use the primary key as a tie breaker in case of duplicates?

Thanks!


Top
 Profile  
 
 Post subject: Re: mapping an ordered collection with dups/gaps in order col
PostPosted: Thu Feb 18, 2010 5:44 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Try with a <bag> instead of <list> and use the order-by attribute for the sorting. Something like:

Code:
<bag
  name="vendorSizeValues"
  inverse="true"
  cascade="all"
  order-by="SORT_ORDER asc, ID asc"
  >
   <key column="VENDOR_SIZE_CHART_ID" not-null="true"/>
   <one-to-many class="com.bluefly.apps.manager.domain.VendorSizeValue" />
</bag>


Top
 Profile  
 
 Post subject: Re: mapping an ordered collection with dups/gaps in order col
PostPosted: Thu Feb 18, 2010 10:57 am 
Newbie

Joined: Fri Feb 05, 2010 3:28 pm
Posts: 5
Thanks! What should the type of the collection be in the POJO? Should it be a List or a SortedSet?


Top
 Profile  
 
 Post subject: Re: mapping an ordered collection with dups/gaps in order col
PostPosted: Thu Feb 18, 2010 4:45 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
A <bag> gives you a list. I guess a <set> mapping could be used instead if you prefer that. If you use the <set order-by...> you'll get a LinkedHashSet. You can also use <set ... sort="..."> where sort is the class name of a Comparator implementation. In the latter cse, the sorting is done in memory and gives you a SortedSet. See http://docs.jboss.org/hibernate/stable/ ... ons-sorted for some more examples.


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.