1

In my java code, I created a tree, where a node in the tree is of type Node. The node has a name, attribute and children of type Node. I am using db4o in order to store the tree. I am doing that by simply storing the root node of the tree. However, I found out that db4o does not store all the children nodes of an object node. When I retrieve the root from the database, and traverse the tree, I can only traverse up to 3 levels of the tree. It seems that the children nodes at lower levels are lost. Can someone help me so I don't lose any node? Thank you.

Below is my code:

Node node1= new Node("root","this is the root",new ArrayList<Node>());
Node node2= new Node("zaid","123",new ArrayList<Node>());
Node node3= new Node("saad","999",new ArrayList<Node>());        
Node node4= new Node("safia","555",new ArrayList<Node>());
Node node5= new Node("ahmad","000",new ArrayList<Node>());

node1.getChildren().add(node2);
node2.getChildren().add(node3);
node3.getChildren().add(node4);
node4.getChildren().add(node5);

ObjectContainer db= Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(),"db");
db.store(node1);

Node node= new Node("root",null,null);
List<Node> result= db.queryByExample(node);
node= result.get(0);
System.out.println(node.getName()
        +","+node.getChildren().get(0).getName()
        +","+node.getChildren().get(0).getChildren().get(0).getName()
        +","+node.getChildren().get(0).getChildren().get(0).getChildren().get(0).getName());

I am getting an exception at the last line of code saying the following:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83
  • What makes you think you're traversing 3 levels of the tree ? – Sridhar Apr 21 '13 at 12:50
  • I am not sure if this information is accurate (the error always occurs after traversing 3 levels), but I am totally sure that I got an error after trying to display the node at the 4th level of the tree, where the traversal starts from the root, and repeats for every children. – Traveling Salesman Apr 21 '13 at 13:10
  • 1
    you didnt perform commit? – vels4j May 15 '13 at 08:24

1 Answers1

0

As It work for me, I'm showing you a complet working example :

import java.util.*;

class Node
{
  String _name;
  public String getName() {return _name;}
  public void setName(final String name) { _name = name;}

  String _value;
  public String getValue() {return _value;}
  public void setValue(final String value) { _value = value;}

  List<Node> _children;
  public List<Node> getChildren() {return _children;}
  public void setChildren(final List<Node> children) { _children = children;}

  Node(final String name, final String value, final List<Node> children)
  {
    setName(name);
    setValue(value);
    setChildren(children);
  }
}

Then you define the main class :

import java.util.*;
import com.db4o.*;
import com.db4o.query.*;
import com.db4o.ta.Activatable;

class test
{
  public static void main(String[] argv)
  {
Node node1= new Node("root","this is the root",new ArrayList<Node>());
Node node2= new Node("zaid","123",new ArrayList<Node>());
Node node3= new Node("saad","999",new ArrayList<Node>());        
Node node4= new Node("safia","555",new ArrayList<Node>());
Node node5= new Node("ahmad","000",new ArrayList<Node>());

node1.getChildren().add(node2);
node2.getChildren().add(node3);
node3.getChildren().add(node4);
node4.getChildren().add(node5);

ObjectContainer db= Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(),"db");
db.store(node1);

Node node= new Node("root",null,null);
List<Node> result= db.queryByExample(node);
node= result.get(0);
System.out.println(
             node.getName()
        +","+node.getChildren().get(0).getName()
        +","+node.getChildren().get(0).getChildren().get(0).getName()
        +","+node.getChildren().get(0).getChildren().get(0).getChildren().get(0).getName());
  }

}

and the build / run can be done like this :

javac -classpath "db4o-8.0.249.16098-all-java5.jar:." *.java
java -classpath "db4o-8.0.249.16098-all-java5.jar:." test

You can have more information on db4o -> documentation -> tutorial 8.0 . While the 8.1 as been release, there is no special tutorial.

Galigator
  • 8,957
  • 2
  • 25
  • 39
  • If you still experiments problems you can force the "Activation Depth" in your configuration instead of using default configuration (configuration.common().activationDepth(10);). You can also look at "transparent activation" because it look like it will be your next question. – Galigator Jul 03 '13 at 13:15