Question

How can DB4o users get back the data for only one object?

(This is akin to getting the data for only one row of a traditional relational database table.)

With DB4o, I only know how to get the data back for a class of objects but not simply one unique object instance.

Was it helpful?

Solution

just query objects and get first item out of the result (the same like in relational database)

to get it by Guid ID:

using (IObjectContainer session = this.GetNewSession())
{
    Dummy result = (from Dummy item in session
                    where item.Id == Guid.Parse("....")
                    select item).FirstOrDefault()
}

the result will be either null if item doesn't exist or the object found

other option is to get it directly by internal ID such as (or even UUID):

long id = ....;
using (IObjectContainer session = this.GetNewSession())
{
    Dummy result = (Dummy)session.Ext().GetByID(id);
}

OTHER TIPS

I have answered my own question (I believe):

Solution #1:

public List<Object> getListOfObjects(final Object o){

    List<Object> result = db.query(new Predicate<Object>(){

      @Override
      public boolean match (Object arg0){

         if(arg0.equals(o)){
            return true;
          }
        else{
            return false;
         }
    });

    return result;
  }

Solution #2:

public ObjectSet<Class<?>> getListOfObjects(Object o){

      Query q = db.query();
      q.constrain(o);
      ObjectSet<Class<?>> set = q.execute();

  return set;
}

Maybe someone knows if one of these solutions is better than the other, or whatever.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top