Question

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.

Was it helpful?

Solution 2

This was actually a package import problem. For some reason when using db4o to say...

import com.db4o.*;

doesn't resolve dependencies that saying....

import com.db4o.query.Predicate.

...does. Strange....

OTHER TIPS

Use the direct query counterpart. It will retrieve all instances of the given class. And it interacts also better with Java generics =)

List<Node> nodes = db.query(Node.class);
// do stuff
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top