-->
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: Setting empty collections to null when retrieving
PostPosted: Sun Nov 21, 2004 4:23 pm 
Newbie

Joined: Sun Nov 21, 2004 4:07 pm
Posts: 2
Location: Grand Rapids, Michigan
I am working with a number of different parent/child tables which I have successfully mapped as sets in hibernate. My application works beautifully except that Session.get creates zero-element sets when there are no rows in the child tables. Unfortunately, my application requires that in such circumstances the collections be set to null instead. How can I accomplish this?

I apologize in advance if this is documented somewhere, but I have reviewed the documentation and several other books and can find no answer to this question.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 21, 2004 11:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Hibernate needs the collection reference in order to track additions to the collection. If your application requires empty collections to be presented as null, your best bet would be to configure the mapping such that Hibernate directly accesses the fields; then your applications can access through the property getter which does:
Code:
public Set getMyCollection() {
    if (myCollection.isEmpty()) {
        return null;
    }
    else {
        return myCollection;
    }
}


You can also modify the setter to "swap" the empty collection with null, but that will cause Hibernate to attempt to delete the collection during flush. I'd, obviously, not recommend this approach ;)


Top
 Profile  
 
 Post subject: Thanks.
PostPosted: Mon Nov 22, 2004 6:54 pm 
Newbie

Joined: Sun Nov 21, 2004 4:07 pm
Posts: 2
Location: Grand Rapids, Michigan
Thanks for the suggestion, Steve. I think the real solution, though, is to re-factor my domain code to use zero-length collections instead of nulls. Oh well. It was shaping up to be a pretty boring week otherwise. ;-)


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.