-->
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.  [ 1 post ] 
Author Message
 Post subject: Private Getters/Setters w/ Field Access & Lazy Loading
PostPosted: Mon Feb 06, 2006 3:30 pm 
Newbie

Joined: Sat Jan 07, 2006 5:18 pm
Posts: 19
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.