문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top