You are here: Basics Operations & Concepts > Querying > Query By Example > Query By Example Basics

Query By Example Examples

Query By Example Basics

For a query you pass in an example object to db4o. db4o will exanimate the object with reflection. Each field which doesn't have the default value will be use as a constrain. This means a number-field which isn't zero, a reference which isn't null or a string which isn't null. In this example we set the name to John. Then db4o will return all pilots with the name John.

Pilot theExample = new Pilot();
theExample.setName("John");
final ObjectSet<Pilot> result = container.queryByExample(theExample);
QueryByExamples.java: Query for John by example

Or we set the age to 33 and db4o will return all 33 years old pilots.

Pilot theExample = new Pilot();
theExample.setAge(33);
final ObjectSet<Pilot> result = container.queryByExample(theExample);
QueryByExamples.java: Query for 33 year old pilots

Combine Values

When you set multiple values all will be used as constrain. For example when we set the name to Jo and the age to 29 db4o will return all pilots which are 29 years with the name Jo.

Pilot theExample = new Pilot();
theExample.setName("Jo");
theExample.setAge(29);
final ObjectSet<Pilot> result = container.queryByExample(theExample);
QueryByExamples.java: Query a 29 years old Jo

All Objects Of A Type

If you pass a empty example db4o will return all objects of that type.

Pilot example = new Pilot();
final ObjectSet<Pilot> result = container.queryByExample(example);
QueryByExamples.java: All objects of a type by passing a empty example

Alternatively you also can directly pass in the type.

final ObjectSet<Pilot> result = container.queryByExample(Pilot.class);
QueryByExamples.java: All objects of a type by passing the type

All Objects

When you pass null all objects stored in the database will be returned.

final ObjectSet<Pilot> result = container.queryByExample(null);
QueryByExamples.java: All objects

Nested Objects

You can also use nested objects as an example. For example with a car and a pilot. We can query for a car which has a pilot with certain constrains. In this example we get the cars which pilot is called Jenny.

Pilot pilotExample = new Pilot();
pilotExample.setName("Jenny");

Car carExample = new Car();
carExample.setPilot(pilotExample);
final ObjectSet<Car> result = container.queryByExample(carExample);
QueryByExamples.java: Nested objects example

Contains Example

Collections and arrays act a little different. Query by example returns all object which have at least the items in the array or collection from the example. For example it returns the blog post which has the "db4o"-tag in its tag-collection.

BlogPost pilotExample = new BlogPost();
pilotExample.addTags("db4o");
final ObjectSet<Car> result = container.queryByExample(pilotExample);
QueryByExamples.java: Contains in collections

Structured Contains

You can even check that a item in a collection fulfills certain criteria's. For example we can check that the blog post has an author with the name John in its author-collection.

BlogPost pilotExample = new BlogPost();
pilotExample.addAuthors(new Author("John"));
final ObjectSet<Car> result = container.queryByExample(pilotExample);
QueryByExamples.java: Structured contains