-->
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.  [ 6 posts ] 
Author Message
 Post subject: stupid problem with sorting
PostPosted: Mon Sep 29, 2003 3:12 pm 
Regular
Regular

Joined: Fri Aug 29, 2003 12:48 pm
Posts: 63
I'm trying to maintain a sorted set of Subjects in a Study object. Subjects should be sorted by their name property. I have been using the order-by attribute and a LinkedHashSet, but this results in an improperly sorted set immediately after adding a new Subject.

I thought the proper workaround would have been to use the sort="natural" attribute, a SortedSet object, and implement Comparable on the Subject object, but I'm getting an NPE when I try that. The compareTo method reads:

Code:
    public int compareTo(Object object) {
        Subject subject = (Subject)object;
        return getName().compareTo(subject.getName());
    }


The relevant portion of the stack trace reads:

Code:
java.lang.NullPointerException
   at com.rhoworld.edc.model.Subject.compareTo(Subject.java:66)
   at java.util.TreeMap.compare(TreeMap.java:1081)
   at java.util.TreeMap.put(TreeMap.java:459)
   at java.util.TreeSet.add(TreeSet.java:205)
   at java.util.AbstractCollection.addAll(AbstractCollection.java:315)
   at java.util.TreeSet.addAll(TreeSet.java:251)
   at net.sf.hibernate.collection.Set.endRead(Set.java:252)
   at net.sf.hibernate.loader.Loader.doFind(Loader.java:200)
   at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:602)
   at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:102)
   at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:2897)
   at net.sf.hibernate.collection.PersistentCollection.getInitialValue(PersistentCollection.java:128)
   at net.sf.hibernate.type.PersistentCollectionType.getCollection(PersistentCollectionType.java:74)
   at net.sf.hibernate.type.PersistentCollectionType.resolveIdentifier(PersistentCollectionType.java:177)
   at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:1959)
   at net.sf.hibernate.loader.Loader.doFind(Loader.java:196)
   at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:587)
   at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:42)
   at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:396)
   at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:1889)
   at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1757)
   at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1688)


My theory is that the name property hasn't been loaded from the database yet, but hibernate is going ahead and stuffing the object into a collection (presumably the Study's since I'm not yet using lazy loading on these collections.)

Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2003 3:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yes, this can occur. It occurs less often in more recent versions of Hibernate, but I can't do anything to make it go away completely.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2003 3:38 pm 
Regular
Regular

Joined: Fri Aug 29, 2003 12:48 pm
Posts: 63
What's the suggested remedy then? I'd reckon the thing to do would be go with order-by (faster and better in general?), but to re-initialize the collection of Subjects for the given Study before walking it. Can I do that without reloading the entire Study object (and all of its other collections)?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2003 5:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Can I do that without reloading the entire Study object (and all of its other collections)?


No.

Note that the exception above does not usually occur if you map the association with lazy="true".


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2003 5:05 pm 
Regular
Regular

Joined: Fri Aug 29, 2003 12:48 pm
Posts: 63
Thanks for the help. I did go ahead and switch to lazy loading with good results, though the "usually" in your response gives me pause... :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2003 5:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Heh :)

It is still possible to get the exception if there are some crazy circular references and such.

This problem occurs due to a really important optimization, Hibernate's "two phase load".


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