Query By Example

When using Query By Example(QBE) you provide db4o with a template object. db4o will return all of the objects which match all non-default field values. This is done via reflecting all of the fields and building a query expression where all non-default-value fields are combined with AND expressions. Here's a simple example:

PersistentExample.cs: RetrievePilotByName
1public static void RetrievePilotByName(IObjectContainer db) 2 { 3 Pilot proto = new Pilot("Michael Schumacher", 0); 4 IObjectSet result = db.Get(proto); 5 ListResult(result); 6 }

PersistentExample.vb: RetrievePilotByName
1Private Shared Sub RetrievePilotByName(ByVal db As IObjectContainer) 2 Dim proto As Pilot = New Pilot("Michael Schumacher", 0) 3 Dim result As IObjectSet = db.Get(proto) 4 ListResult(result) 5 End Sub

Querying this way has some obvious limitations:

  • db4o must reflect all members of your example object.
  • You cannot perform advanced query expressions. (AND, OR, NOT, etc.)
  • You cannot constrain on values like 0 (integers), "" (empty strings), or nulls (reference types) because they would be interpreted as unconstrained.
  • You need to be able to create objects without initialized fields. That means you can not initialize fields where they are declared. You can not enforce contracts that objects of a class are only allowed in a well-defined initialized state.
  • You need a constructor to create objects without initialized fields.

To get around all of these constraints, db4o provides the Native Query (NQ) system.