-->
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.  [ 3 posts ] 
Author Message
 Post subject: difficulty with sorted set
PostPosted: Tue Feb 22, 2005 9:37 pm 
Newbie

Joined: Mon Sep 06, 2004 1:39 pm
Posts: 18
Location: Utah, USA
I am having difficulty retrieving a sorted set. Before using Hibernate I would expect to receive the rows of the database back in the order they were stored (usually sequential by ID). I have found that when using sets the order in which objects are returned is quite random. In the "mostly" full mapping below I have added the sort="natural", which then gives the error listed. without sort="natural" the mapping works fine, except that the objects are always in some random order.

How can I map a sorted set? I have searched the forum with no luck. I also have hibernate in action which gives examples using collections of Strings. Does the method change when I am retrieving objects other than string??? Thanks in advance...

Hibernate version: 2.1.6

Mapping documents:
<hibernate-mapping package="org.simplecart.shopcart.model">

<class name="CatalogItem"
table="CATALOG_ITEM">

<id name="id"
type="long"
column="CATALOG_ID"
unsaved-value="null">
<generator class="native"/>
</id>

<property name="name"
type="string"
column="NAME"
length="255"/>

<property name="description"
type="text"
column="DESCRIPTION"/>

<joined-subclass
name="Product"
table="PRODUCT">
<key column="PRODUCT_ID"/>

<many-to-one
name="category"
class="Category"
column="CATEGORY_ID"
cascade="save-update"/>

</joined-subclass>

<joined-subclass
name="Category"
table="CATEGORY">
<key column="CATEGORY_ID"/>

<set name="products"
table="PRODUCT"
inverse="true"
lazy="true"
cascade="save-update"
sort="natural">
<key column="CATEGORY_ID"/>
<one-to-many class="Product"/>
</set>


</joined-subclass>

</class>

</hibernate-mapping>

Partial stack trace of any exception that occurs:
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection

Name and version of the database you are using: PostgreSQL 7.4


Top
 Profile  
 
 Post subject: use comparator
PostPosted: Wed Feb 23, 2005 1:09 pm 
Newbie

Joined: Mon Sep 06, 2004 1:39 pm
Posts: 18
Location: Utah, USA
I'll answer my own question:

I wrote a comparator for persistent classes
Code:
import java.util.Comparator;
import org.simplecart.shopcart.model.Persistent;
/**
*
* @author Daniel Watrous
*/
public class PrimaryKeyComparator implements Comparator {
   
    public int compare(Object obj1, Object obj2) {
        int result;
        Persistent pobj1 = (Persistent) obj1;
        Persistent pobj2 = (Persistent) obj2;
       
        // return this result
        return pobj1.getId().compareTo(pobj2.getId());
    }
   
    public boolean equals(Object obj) {
        return this.equals(obj);
    }
}


and I modified my mapping as follows:

<set name="products"
table="PRODUCT"
inverse="true"
lazy="true"
cascade="save-update"
sort="org.simplecart.shopcart.model.PrimaryKeyComparator">
<key column="CATEGORY_ID"/>
<one-to-many class="Product"/>
</set>


Top
 Profile  
 
 Post subject: Re: difficulty with sorted set
PostPosted: Tue Dec 21, 2010 7:52 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
If sorting with a comparator, then the sorting is done in memory and not by the database.

Your collection is not a large one ?

In that case, a sorting by the database would make more sense.


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