Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.1.2
I am looking at ways to protect my domain model using private getters and setters and field access instead of property access (I'm using Annotations). My question is am I going to run into lazy initialization problems if I'm directly accessing fields internally in a class to modify collections? For example, here's a sample class:
Code:
public class Parent
{
@OneToMany(mappedBy="parent")
private Set<Child> children = new HashSet<Children>();
public Set<Child> getChildren()
{ return Collections.unmodifiableSet(children); }
public void addChild(Child obj)
{ children.add(obj); }
public void removeChild(Child obj)
{ children.remove(obj); }
}
public class Child
{
@ManyToOne
@JoinColumn(name="parent")
private Parent parent = null;
}
Will I have problems directly accessing the children private collection via the removeChild() or addChild() methods if the collection is lazily loaded? Do I need to implement a method on Parent like
Code:
private Set<Child> getChildrenInternal() { return children; }
and then update the removeChild() and addChild() methods so that they look like this?
Code:
public void addChild(Child obj)
{ getChildrenInternal().add(obj); }
public void removeChild(Child obj)
{ getChildrenInternal().remove(obj); }
I ran into the private field issue setting equals() and hashCode() methods on my base domain entity where the natural-key used in the methods wasn't being loaded by the proxy since I was accessing the field. Is this going to cause the same problem? Thanks for any help!