// accessDb4o |
// storeFirstPilot |
Retrieving objects
public static void listResult(ObjectSet result) {
System.out.println(result.size());
while(result.hasNext()) {
System.out.println(result.next());
}
}
Query by Example (QBE)
// retrieveAllPilotQBE
Pilot proto=new Pilot(null,0);
ObjectSet result=db.get(proto);
listResult(result);
// retrieveAllPilots
ObjectSet result=db.get(Pilot.class);
listResult(result);
List <Pilot> pilots = db.query(Pilot.class);
Native Queries (NQ)
List <Pilot> pilots = db.query(new Predicate<Pilot>() {
public boolean match(Pilot pilot) {
return pilot.getPoints() == 100;
}
});
List <Pilot> result = db.query(new Predicate<Pilot>() {
public boolean match(Pilot pilot) {
return pilot.getPoints() > 99
&& pilot.getPoints() < 199
|| pilot.getName().equals("Rubens Barrichello");
}
});
SODA Query API (SODA)
// retrieveAllPilots
Query query=db.query();
query.constrain(Pilot.class);
ObjectSet result=query.execute();
listResult(result);
Updating objects
// updatePilot
ObjectSet result=db.get(new Pilot("Michael Schumacher",0));
Pilot found=(Pilot)result.next();
found.addPoints(11);
db.set(found);
System.out.println("Added 11 points for "+found);
retrieveAllPilots(db);
Deleting objects
// deleteFirstPilotByName
ObjectSet result=db.get(new Pilot("Michael Schumacher",0));
Pilot found=(Pilot)result.next();
db.delete(found);
System.out.println("Deleted "+found);
retrieveAllPilots(db);