-->
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: Counting number of nested objects managed by an entity
PostPosted: Mon Apr 06, 2009 7:38 pm 
Beginner
Beginner

Joined: Fri Mar 21, 2008 8:07 pm
Posts: 23
I have a Node entity that contains a set of children Nodes within it. A child node contains a set of children, and a child of it could contain more children. There is potentially an infinite level of nesting, but in practice that would not happen.

What I want to do is count how many total children there are in the entire hierarchy starting from a given node. I'm looking for a solution that is efficient and inexpensive on the database, the app server, and memory utilization.

Here is a simplified version of the Node entity:

Code:
public class Node {
  private Long id;
  private String name;
  private Node parent;
  private Set<Node> children; 
}


I've got things working for small data sets with a method like this in Node, but I'm wondering if there would be a more efficient Hibernate way of getting this data. I'm using DAOs, so I could easily put the query into it rather than asking the Node itself. I haven't tried the following with a large hierarchy yet.

Code:
   public int countChildren() {
      int count = 0;
      Iterator<Node> nodes = children.iterator();
      while (nodes.hasNext()) {
         Node node = nodes.next();
         count += node.countChildren();
         count++;
      }
      return count;
   }


Thanks,
Tauren
[/code]


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.