java - 通过仅存储根对象来在 db4o 中存储树

标签 java database tree db4o

在我的 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

最佳答案

由于它对我有用,我向您展示一个完整的工作示例:

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已经发布,但没有专门的教程。

关于java - 通过仅存储根对象来在 db4o 中存储树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16131055/

相关文章:

Java:控制台跳过输入

java - 如何在 spring 数据 JPA 查询之间编写自定义?

sql - 复合主键与附加 "ID"列?

Mysql 单元包含需要格式化才能用于 IN 语句的包含列表?

c - 通过重复存储单词的最佳树型

c++ - 是否可以组合对称代码段?

java - 数组实例化调用构造函数?

Python从数据库中读取数据并重写到另一个表中

r - 如何比较两个树状图(在 R 中)之间的 "similarity"?

java - 如何在自定义约束 validator 中使用注释元素