【问题标题】:Storing a tree in db4o by just storing the root object通过仅存储根对象在 db4o 中存储树
【发布时间】:2013-04-21 12:23:06
【问题描述】:

在我的 java 代码中,我创建了一棵树,其中树中的一个节点是 Node 类型的。 该节点具有 Node 类型的名称、属性和子节点.我正在使用db4o 来存储树。我通过简单地存储树的根节点来做到这一点。但是,我发现 db4o 并没有存储对象节点的所有子节点。当我从数据库中检索根并遍历树时,我最多只能遍历树的 3 层。似乎较低级别的子节点丢失了。有人可以帮助我,这样我就不会丢失任何节点吗?谢谢。

下面是我的代码:

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

【问题讨论】:

  • 是什么让您认为您正在遍历树的 3 个级别?
  • 我不确定这个信息是否准确(遍历3层后总是出现错误),但我完全确定在尝试显示第4层节点后出现错误树,遍历从根开始,并为每个孩子重复。
  • 你没有执行提交?

标签: java database tree db4o


【解决方案1】:

因为它对我有用,所以我向您展示了一个完整的工作示例:

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 已经发布,没有特别教程。

【讨论】:

  • 如果您仍然遇到问题,您可以在配置中强制“激活深度”,而不是使用默认配置 (configuration.common().activationDepth(10);)。您还可以查看“透明激活”,因为它看起来将是您的下一个问题。
猜你喜欢
  • 1970-01-01
  • 2012-08-18
  • 2012-08-28
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多