Db4o查询的空结果当我指定一个类时,按Example()方法

时间:2012-07-14 00:00:23

标签: java class dynamic classloader db4o

所以我对Db4o API有一个奇怪的问题。我试图从数据库中检索某些类的对象(取决于用户的查询,但这不是真正相关)。数据库肯定包含我想要检索的类的对象。因此,例如,当我查询任何对象的ByBample()时,然后打印出对象集的每个元素的类,就像这样......

public void evaluateQuery(ObjectContainer db) {
    if (this.hasPredicate) {
        ;
    } else {
        if (this.isNode) {
            ObjectSet nodes = db.queryByExample(Object.class);
            ListIterator listIter = nodes.listIterator();
            while (listIter.hasNext()) {
                Object node = listIter.next();
                System.out.println("Object has Class: " + node.getClass());
            }
        }
    }
} 

......我得到了这样的结果......

Object has Class: class SubNode
Object has Class: class SubNode
Object has Class: class SubNode
Object has Class: class SubNode
Object has Class: class SubNode
Object has Class: class Node
Object has Class: class Node
Object has Class: class Node
Object has Class: class Node
Object has Class: class Node
Object has Class: class Node
Object has Class: class Node
Object has Class: class Node
Object has Class: class Node
Object has Class: class Node
Object has Class: class SubNode
Object has Class: class SubNode
Object has Class: class SubNode
Object has Class: class SubNode
Object has Class: class SubNode

...如您所见,数据库具有将类设置为Node和SubNode的对象。现在,我希望代码实际上像这样......

public void evaluateQuery(ObjectContainer db) {
    if (this.hasPredicate) {
        ;
    } else {
        if (this.isNode) {
            ObjectSet<Node> nodes = db.queryByExample(Node.class);
            ListIterator<Node> listIter = nodes.listIterator();
            while (listIter.hasNext()) {
                Node node = listIter.next();
                System.out.println("Object has Class: " + node.getClass());
            }
        }
    }

..但是当我这样做时,ObjectSet总是空的,我似乎无法弄清楚原因。我在运行时使用ClassLoader将一些类的子类(例如上面的Node类)加载到环境中,并且它们肯定在那里,因为我在其他上下文中实例化/使用它们没有问题。

2 个答案:

答案 0 :(得分:1)

使用直接查询对应方。它将检索给定类的所有实例。它与Java泛型相互作用也更好=)

List<Node> nodes = db.query(Node.class);
// do stuff

答案 1 :(得分:0)

这实际上是一个包导入问题。出于某种原因使用db4o说...

import com.db4o。*;

不解决说....的依赖关系

import com.db4o.query.Predicate。

...一样。奇怪....