To retrieve all cars a simple 'blank' prototype can be used.
1private static void retrieveAllCarsQBE(ObjectContainer container) { 2
Car proto = new Car(null); 3
ObjectSet result = container.get(proto); 4
listResult(result); 5
}
Now let's initialize the prototype to specify all cars driven by Rubens Barrichello.
1private static void retrieveCarByPilotQBE(ObjectContainer container) { 2
Pilot pilotproto = new Pilot("Rubens Barrichello", 0); 3
Car carproto = new Car(null); 4
carproto.setPilot(pilotproto); 5
ObjectSet result = container.get(carproto); 6
listResult(result); 7
}
What about retrieving a pilot by car? You simply don't need that -if you already know the car, you can simply access the pilot field directly.
1private static void retrieveCarByPilotQBE(ObjectContainer container) { 2
Pilot pilotproto = new Pilot("Rubens Barrichello", 0); 3
Car carproto = new Car(null); 4
carproto.setPilot(pilotproto); 5
ObjectSet result = container.get(carproto); 6
listResult(result); 7
}
Using native queries with constraints on deep structured objects is
straightforward, you can do it just like you would in plain other code.
Let's constrain our query to only those cars driven by a Pilot with a
specific name:
01private static void retrieveCarsByPilotNameNative( 02
ObjectContainer container) { 03
final String pilotName = "Rubens Barrichello"; 04
ObjectSet results = container.query(new Predicate<Car>() { 05
public boolean match(Car car) { 06
return car.getPilot().getName().equals(pilotName); 07
} 08
}); 09
listResult(results); 10
}
In order to use SODA for querying for a car given its pilot's name you have to descend two levels into our query.
1private static void retrieveCarByPilotNameQuery( 2
ObjectContainer container) { 3
Query query = container.query(); 4
query.constrain(Car.class); 5
query.descend("pilot").descend("name").constrain( 6
"Rubens Barrichello"); 7
ObjectSet result = query.execute(); 8
listResult(result); 9
}
You can also constrain the pilot field with a prototype to achieve the same result.
1private static void retrieveCarByPilotProtoQuery( 2
ObjectContainer container) { 3
Query query = container.query(); 4
query.constrain(Car.class); 5
Pilot proto = new Pilot("Rubens Barrichello", 0); 6
query.descend("pilot").constrain(proto); 7
ObjectSet result = query.execute(); 8
listResult(result); 9
}
Descending into a query provides you with another query. Starting out from a query root you can descend in multiple directions. In practice this is the same as ascending from one child to a parent and descending to another child. The queries turn one-directional references in objects into true relations. Here is an example that queries for "a Pilot that is being referenced by a Car, where the Car model is 'Ferrari'":
1private static void retrievePilotByCarModelQuery( 2
ObjectContainer container) { 3
Query carquery = container.query(); 4
carquery.constrain(Car.class); 5
carquery.descend("model").constrain("Ferrari"); 6
Query pilotquery = carquery.descend("pilot"); 7
ObjectSet result = pilotquery.execute(); 8
listResult(result); 9
}