01: package com.db4o.f1.chapter4;
02:
03: import java.io.*;
04: import java.util.Arrays;
05:
06: import com.db4o.*;
07: import com.db4o.f1.*;
08: import com.db4o.query.*;
09:
10: public class InheritanceExample extends Util {
11: public static void main(String[] args) {
12: new File(Util.DB4OFILENAME).delete();
13: ObjectContainer db = Db4o.openFile(Util.DB4OFILENAME);
14: try {
15: storeFirstCar(db);
16: storeSecondCar(db);
17: retrieveTemperatureReadoutsQBE(db);
18: retrieveAllSensorReadoutsQBE(db);
19: retrieveAllSensorReadoutsQBEAlternative(db);
20: retrieveAllSensorReadoutsQuery(db);
21: retrieveAllObjectsQBE(db);
22: } finally {
23: db.close();
24: }
25: }
26:
27: public static void storeFirstCar(ObjectContainer db) {
28: Car car1 = new Car("Ferrari");
29: Pilot pilot1 = new Pilot("Michael Schumacher", 100);
30: car1.setPilot(pilot1);
31: db.set(car1);
32: }
33:
34: public static void storeSecondCar(ObjectContainer db) {
35: Pilot pilot2 = new Pilot("Rubens Barrichello", 99);
36: Car car2 = new Car("BMW");
37: car2.setPilot(pilot2);
38: car2.snapshot();
39: car2.snapshot();
40: db.set(car2);
41: }
42:
43: public static void retrieveAllSensorReadoutsQBE(ObjectContainer db) {
44: SensorReadout proto = new SensorReadout(null, null, null);
45: ObjectSet result = db.get(proto);
46: listResult(result);
47: }
48:
49: public static void retrieveTemperatureReadoutsQBE(ObjectContainer db) {
50: SensorReadout proto = new TemperatureSensorReadout(null, null,
51: null, 0.0);
52: ObjectSet result = db.get(proto);
53: listResult(result);
54: }
55:
56: public static void retrieveAllSensorReadoutsQBEAlternative(
57: ObjectContainer db) {
58: ObjectSet result = db.get(SensorReadout.class);
59: listResult(result);
60: }
61:
62: public static void retrieveAllSensorReadoutsQuery(ObjectContainer db) {
63: Query query = db.query();
64: query.constrain(SensorReadout.class);
65: ObjectSet result = query.execute();
66: listResult(result);
67: }
68:
69: public static void retrieveAllObjectsQBE(ObjectContainer db) {
70: ObjectSet result = db.get(new Object());
71: listResult(result);
72: }
73:
74: }
|