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