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.  [ 2 posts ] 
Author Message
 Post subject: Best Practices: "Encapsulate Collection"
PostPosted: Mon Apr 03, 2006 1:50 pm 
Newbie

Joined: Wed Apr 20, 2005 1:33 pm
Posts: 6
I am trying to design an entity class with the "Encapsulate Collection" refactoring, using Collections.unmodifiableSet().

http://www.refactoring.com/catalog/enca ... ction.html

However, I have been encountering all sorts of difficulty with this approach, as Hibernate is throwing NullPointerExceptions at various places. I understand that Hibernate uses its own Collections implementations (PersistentSet, etc.), but I am not exactly sure how this should impact my code.

Rather troubleshooting the specific errors I am seeing, I would like to back up a bit and make sure I am taking the recommended approach in my class design.

The entity in question has a 1-to-many bi-directional association (i.e. Parent-Children). Based on the errors I have encountered, it seems that Hibernate requires something like the following:

Code:
public class Parent {

    Set children = new HashSet();

    // protected setter for Hibernate
    protected void setChildren(Set children) {
            this.children = children;
    }
   
    public Set getChildren() {           
           return children;

           // this implementation would cause errors
           // return Collections.unmodifiableSet(children);
    }

   
   // "work around" method - not a mapped property
   public Set getAllChildren() {
        return Collections.unmodifiableSet(children);
   }

   // another solution I've seen - defensive copy
   public Iterator getChildrenIterator() {
         List temp = new ArrayList(children);
         return temp.iterator();
   }


}


What is the a recommended Best Practice for encapsulating collections with Hibernate?


Related question:

What is the recommended way to ensure that the collections are always initialized? The Hibernate reference seems to advocate explicitly assigning a new collection instance to the field via the property setter (23.3. Hibernate Code - BlogMain.createBlog), but that feels wrong somehow. Why not initialize the the collection in the constructor, field declaration, or lazily in the method? My question is: what pitfalls await me if I take one of these three approaches?


Top
 Profile  
 
 Post subject: Solution
PostPosted: Sat May 13, 2006 5:24 pm 
Newbie

Joined: Wed Apr 20, 2005 1:33 pm
Posts: 6
Answer: Hibernate does not allow you to use the "Encapsulate Collections" refactoring.

Source:

http://forum.hibernate.org/viewtopic.php?t=938019


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