0

So I'm having a strange problem with the Db4o API. I'm trying to retrieve certain classes of objects (depending on a user's query, but that's not really relevant), from a database. The database definitely has objects with the classes I want to retrieve. So for example, when I queryByExample() for just any Object, and then print out the Classes of each element of the object set, like so...

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

... and I get a result like this...

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

...As you can see, the database has object with Classes set to Node and SubNode. Now, I'd like the code to actually work like this...

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

.. but when I do this, the ObjectSet is invariably empty, and I can't seem to figure out why. I load a certain subset of Classes, such as the above Node Class, into the environment with ClassLoader at runtime, and they're definitely in there, as I instantiate/use them in other contexts with no problems.

4

2 回答 2

0

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

导入 com.db4o.*;

没有解决依赖关系,说....

导入 com.db4o.query.Predicate。

...做。奇怪的....

于 2012-07-15T18:53:48.673 回答
0

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

List<Node> nodes = db.query(Node.class);
// do stuff
于 2012-07-14T09:13:59.523 回答