-->
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: design issue - initialization of collections on pojos
PostPosted: Fri Apr 06, 2007 2:07 am 
Newbie

Joined: Fri Apr 06, 2007 1:54 am
Posts: 14
Hello

This is a design problem I have when using hibernate.

I have the following class:

Code:
public class Foo {

  private long id;
  private List children;

  public Foo() {
  }

...

}

When I fetch a Foo from the database, the children List is initialized to an ArrayList from hibernate. When using the new keyword though, children is uninitialized.
What I don't want to do is what I've been doing always, initializing the collections in the default constructor or instance initializer block:
Code:
public class Foo {

  private long id;
  private List children = new ArrayList();

  public Foo() {
  }

...

}

I don't want to do this because that way I'm creating an ArrayList which will be replaced by hibernate's ArrayList (when getting Foo from the database)

Some ways that I've used so far include:
a) Factory method for construction of the object in correct state.
b) Another constructor
c) providing an init() method which has to be called from client code to set the instance into correct state
d) lazy initializing those collections:
Code:
public List getChildren() {
  if (children==null) {
    children=new ArrayList();
  }
  return children;
}


Do you use any of the methods above? Or should I just not care about that extra ArrayList being wasted?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 8:49 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
why not just do a normal POJO where no extra ArrayList is created

Code:
private List children;
public Foo() {
}

public void setChildren(List list) {
this.children=list;
}
public List getChildren() {
return this.children;
}

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 11:59 am 
Newbie

Joined: Fri Apr 06, 2007 1:54 am
Posts: 14
I want the children collection initialized. In my toString method for example I might have:
Code:
public String toString() {
  return "Foo with " + children.size() + " children";
}

So I don't want to be checking for null everywhere.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 12:20 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
weird you'd call toString on an empty object, or one that hasn't been loaded from the database.

but I guess just don't worry about the ArrayList.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


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.