-->
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.  [ 7 posts ] 
Author Message
 Post subject: Known override equals() - NullPointerException workaround?
PostPosted: Mon Sep 15, 2003 7:09 pm 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:55 pm
Posts: 29
Referring to the known bug in hibernate -http://hibernate.org/117.html#A14 where overriding equals() or using a custom comparator in a SortedSet - I know the listed workaround is setting the collection to be lazy. But my application needs to load the data all at once, inside a transaction, so just having the collection lazily initialized is not going to work.

I have an Entry object, which has a collection (a SortedSet) which has this error when it loads. I've worked on this for quite a while, and I can't find a decent workaround. I tried marking the collection as lazy, implementing the Lifecycle interface, and using Hibernate.load() to load the collection, but that also just throws a NullPointerException. I went as far as making the SortedSet a Bag, then sorting it upon the first getCollection() call to the object, but that causes an entirely different problem where sorting the bag causes updates and deletes to be issued to the database.

The only solution left is a total hack - every time I load an Entry object, I can add a line in my code that will load the lazily initialized SortedSet. But in addition to completely breaking the encapsulation of the Entry object, this would require finding every place in my code where there's any kind of reference to an Entry, including objects which refer to objects which refer to an Entry, and adding code to load the entry - a bug-ridden, inflexible, and time consuming process. Is there a decent hibernate workaround to this problem?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2003 9:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
I have stopped using sets in my domain entities for this exact reason (actually I always used lists before using other O/R tools, but tried sets when starting with Hibernate).

Another option might be to have two "bounded" properties; one mapped in Hibernate, and another accessed from the domain clients. Your entity class could then manage when/how references are placed in the Hibernate mapped property.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 10:33 am 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:55 pm
Posts: 29
Do you mean like have a public getCollection() method, but have a different method, like getH_Collection(), that hibernate actually uses to get/set the data, then on the first call to getCollection() sort the collection, cache it, and return that new collection on all calls to getCollection()?

That would work, and it meets my requirement. (If I didn't understand your suggestion correctly, please correct me) Thanks for your suggestion.

It keeps the details inside my class. It would nice if I didn't have to rework my data model just to keep hibernate happy. :-(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 10:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Thats exactly what I meant.

Quote:
It would nice if I didn't have to rework my data model just to keep hibernate happy


From what I have found, this is almost always re-workable in the application layer above hibernate. I said I no longer uses sets... Well thats not exactly true. I still use them for situations where the cascading creates to the child collection is not needed. This is mainly scenarios depicted on the UI as pick-lists. There, all the possible collection lements are pre-existing and issues with the hashCode(), etc are not really an issue.

For situations where I do need to "cascade" persistence operations across the child collection, I generally simply use bags, although you could also manually call session.saveOrUpdate() for each collection element prior to persisting the parent.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 6:05 pm 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:55 pm
Posts: 29
I'm not entirely sure I understand your post. My problem isn't with saving the object, I get the NullPointerException when loading the object.

I either have to rework my data model or my application to get it to work, I'm not the fondest of that. But at least it's doable.

Someone suggested to me today that I try turning off outer joins - which I did in the global settings file, and afterwords everything worked fine, with no NullPointerException problems. So I tried turning off outer joins for just the collection that throws the NullPointerException -
<many-to-one name="user" column="user_id" outer-join="false" />
But no dice, I still get the exception.

Let me explain my model a little - a Category belongs to a User. However, an Entry has a SortedSet of categories. For some reason when I load an Entry directly, things are just fine. But a comment belongs to an entry. And when I load a Comment, it causes the Entry to be loaded, which loads all of the comments for that entry - and then I get the NullPointerException. What might work is if I could somehow say "For Entry's SortedSet of Comments, don't use an outer-join to load it". I looked through the docs, and SortedSet doesn't have an outer-join property. Would this be possible?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 8:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Sorry, forgot the <diatribe/> tags ;o) I was just going into details about ways I get around using sets.

Quote:
looked through the docs, and SortedSet doesn't have an outer-join property

Not sure what you mean here. SortedSet as in java.util.SortedSet? Are you talking about the J2SE javadocs? A sorted set in Hibernate is simply represented with a <set> mapping with an included sort attribute. And, yes, <set> does also have an outer-join attribute.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 5:40 pm 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:55 pm
Posts: 29
I was talking about a Set mapping in Hibernate with sort="natural". I've tried, but I can't figure out where the "set" mapping has an outer-join property. I believe the correct dtd for a mapping file is:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

And the hibernate-mapping-2.0.dtd file included with hibernate has this for set:

<!ELEMENT set (
meta*,
jcs-cache?,
key,
(element|one-to-many|many-to-many|composite-element|many-to-any)
)>
<!ATTLIST set name CDATA #REQUIRED>
<!ATTLIST set table CDATA #IMPLIED> <!-- default: name -->
<!ATTLIST set schema CDATA #IMPLIED> <!-- default: none -->
<!ATTLIST set lazy (true|false) "false">
<!ATTLIST set sort CDATA "unsorted"> <!-- unsorted|natural|"comparator class" -->
<!ATTLIST set inverse (true|false) "false">
<!ATTLIST set cascade (none|all|save-update|delete|all-delete-orphan) #IMPLIED> <!-- default: none -->
<!ATTLIST set order-by CDATA #IMPLIED> <!-- default: none -->
<!ATTLIST set where CDATA #IMPLIED> <!-- default: none -->


I don't see outer join. I'm using hibernate 2.0.3 - it is in the 2.1 release or something?


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