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.  [ 4 posts ] 
Author Message
 Post subject: Initializing POJO's generic Set
PostPosted: Mon May 08, 2006 11:14 am 
Newbie

Joined: Mon May 08, 2006 10:47 am
Posts: 1
Hibernate version: 3.1.3

I'm trying to figure out how to initialize my POJO's generic Set property (it's children, if you will).

Here's the situation...

Class Parent is as follows (my apologies for syntax errors):

Code:
package x.y.foo;

public class Parent implements java.io.Serializable {

    static final long serialVersionUID = -123L;

    private String propertyA;
    private String propertyB;
    private Set setOfChildren;  //of type x.y.foo.Child

    public Parent() {
    }

    public void setPropertyA(String str) {
        this.propertyA = str;
    }

    public String getPropertyA() {
        return this.propertyA;
    }

    public void setPropertyB(String str) {
        this.propertyB = str;
    }

    public String getPropertyB() {
        return this.propertyB;
    }

    //  Of what concrete implementation must newSet be
    //   inorder for the Hibernate persistence to work?
    public void setSetOfChildren(Set newSet) {
        this.setOfChildren = newSet;
    }

    public Set getSetOfChildren() {
        return this.setOfChildren;
    }

    // Convenience Method
    public void addChildToSetOfChildren(Child child) {
        if (setOfChildren == null) {
            // I would like to initialize setOfChildren here
            //  BUT what concrete implementation should I use???
        }
        this.setOfChildren.add(child);
    }
}


In the example above I would like to know what concrete implementation of Set to use to initialize the Set of children.
I know that Hibernate is using org.hibernate.collection.PersistentSet and I can use this as well BUT that will then tightly
couple my POJO to a Hibernate implementation and I would like to avoid this if possible.

Thank you for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 11:29 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
You can use any type of set that you want when you create the object (I typically use a HashSet) but be aware that when you load the object back in Hibernate will recreate it with a PersistentSet so any special properties of the set that you used will be lost.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 11:32 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
See here for a more complete explanation:- http://www.hibernate.org/hib_docs/v3/re ... persistent


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 3:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
and don't just do the check in the setter; you should also return a collection (not null) from the getter.

_________________
Max
Don't forget to rate


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