2013-04-21 59 views
1

在我的java代码中,我创建了一棵树,树中节点的类型为节点。该节点具有名称,属性和子类型节点。我使用db4o来存储树。我正在通过简单地存储树的根节点来实现这一点。但是,我发现db4o不存储对象节点的所有子节点。当我从数据库中检索根,并遍历树时,我只能遍历树的3个层次。看来,较低级别的儿童节点已经丢失。有人可以帮助我,所以我不会失去任何节点?谢谢。只需存储根对象即可在db4o中存储树

下面是我的代码:

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()); 

我得到一个异常在代码的最后一行说以下内容:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
+0

是什么让你觉得你正在遍历树的三个层次? – Sridhar 2013-04-21 12:50:25

+0

我不确定这些信息是否准确(错误总是在遍历3个级别后发生),但我完全确定在尝试在树的第4级显示节点后出现错误,此时遍历从根,并为每个孩子重复。 – 2013-04-21 13:10:23

+1

你没有执行提交? – vels4j 2013-05-15 08:24:27

回答

0

,因为它为我工作,我向你们表示结束于工作示例:

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); 
    } 
} 

然后定义主类:

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()); 
    } 

} 

和构建/运行可以这样做:

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

你可以有db4o的更多信息 - >文档 - >教程8.0。虽然8.1版本已经发布,但没有特别的教程。

+0

如果您仍在试验问题,则可以在配置中强制使用“激活深度”,而不是使用默认配置(configuration.common()。activationDepth(10);)。你也可以看看“透明激活”,因为它看起来像是你的下一个问题。 – Galigator 2013-07-03 13:15:40