-->
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.  [ 7 posts ] 
Author Message
 Post subject: Unmodifiable child collections
PostPosted: Tue Dec 21, 2004 4:11 am 
Regular
Regular

Joined: Thu Nov 13, 2003 2:55 am
Posts: 71
Location: Sweden
I'm currently reading the reference manual and have some thoughts considering the parent/child model in chapter 9 (version 2.1.1). I want to use the principles in the example (9.2), but I also want to make sure nobody adds a child without calling addChild() but instead getChildren().add() - and thus possibly not setting the parent. Therefore I am thinking I would modify getChildren() to return an unmodifiable Set instead.

Code:
public class Parent {
  ...
  private Set children
  ...

  public Set getKittens() {
      return java.util.Collections.unmodifiableSet(kittens);
  }

  public void addChild(Child c) {
    c.setParent(this);
    children.add(c);
  }
}

For the mapping to work properly, I assume I need to set
Code:
access="field"
on the property. (access="field" works for private properties, right?)

Would this generally be a good idea?
Could it cause any problems with lazy initialization?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 5:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i haven't tried it, but i don't think it would be a good idea!

Instead don't have the getChildren public (make it private) or simply as you say use field access to protect it.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 5:24 am 
Regular
Regular

Joined: Thu Nov 13, 2003 2:55 am
Posts: 71
Location: Sweden
max wrote:
Instead don't have the getChildren public (make it private) or simply as you say use field access to protect it.


So then how would the rest of the application access the children (for reading)???
Through a getUnmodifiableChildren() with the abovementioned implementation? What would be the difference to using field mapping?


Top
 Profile  
 
 Post subject: Re: Unmodifiable child collections
PostPosted: Tue Dec 21, 2004 5:27 am 
Regular
Regular

Joined: Thu Nov 13, 2003 2:55 am
Posts: 71
Location: Sweden
mate wrote:
Code:
public Set getKittens() {
      return java.util.Collections.unmodifiableSet(kittens);
  }



This should of course read
Code:
public Set getChildren() {
      return java.util.Collections.unmodifiableSet(children);
  }


(I obviously mixed two examples)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 6:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
just map it as access="field" and you can have whatever getter you want.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 6:43 am 
Regular
Regular

Joined: Thu Nov 13, 2003 2:55 am
Posts: 71
Location: Sweden
max wrote:
just map it as access="field" and you can have whatever getter you want.


That was my initial thought, which you said wasn't a good idea.
I'm sorry, but I just don't follow...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 6:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
oh sorry - i answered to fast.

I just noticed the concurrent sets, not the access="field" - my bad ;)

just do it then ;)

_________________
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.  [ 7 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.