Source Code Cross Referenced for StructuredExample.java in  » Database-DBMS » db4o-6.4 » com » db4o » f1 » chapter2 » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » db4o 6.4 » com.db4o.f1.chapter2 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.db4o.f1.chapter2;
002:
003:        import java.io.File;
004:
005:        import com.db4o.Db4o;
006:        import com.db4o.ObjectContainer;
007:        import com.db4o.ObjectSet;
008:        import com.db4o.f1.Util;
009:        import com.db4o.query.Predicate;
010:        import com.db4o.query.Query;
011:
012:        public class StructuredExample extends Util {
013:            public static void main(String[] args) {
014:                new File(Util.DB4OFILENAME).delete();
015:                ObjectContainer db = Db4o.openFile(Util.DB4OFILENAME);
016:                try {
017:                    storeFirstCar(db);
018:                    storeSecondCar(db);
019:                    retrieveAllCarsQBE(db);
020:                    retrieveAllPilotsQBE(db);
021:                    retrieveCarByPilotQBE(db);
022:                    retrieveCarByPilotNameQuery(db);
023:                    retrieveCarByPilotProtoQuery(db);
024:                    retrievePilotByCarModelQuery(db);
025:                    updateCar(db);
026:                    updatePilotSingleSession(db);
027:                    updatePilotSeparateSessionsPart1(db);
028:                    db.close();
029:                    db = Db4o.openFile(Util.DB4OFILENAME);
030:                    updatePilotSeparateSessionsPart2(db);
031:                    db.close();
032:                    updatePilotSeparateSessionsImprovedPart1();
033:                    db = Db4o.openFile(Util.DB4OFILENAME);
034:                    updatePilotSeparateSessionsImprovedPart2(db);
035:                    db.close();
036:                    db = Db4o.openFile(Util.DB4OFILENAME);
037:                    updatePilotSeparateSessionsImprovedPart3(db);
038:                    deleteFlat(db);
039:                    db.close();
040:                    deleteDeepPart1();
041:                    db = Db4o.openFile(Util.DB4OFILENAME);
042:                    deleteDeepPart2(db);
043:                    deleteDeepRevisited(db);
044:                } finally {
045:                    db.close();
046:                }
047:            }
048:
049:            public static void storeFirstCar(ObjectContainer db) {
050:                Car car1 = new Car("Ferrari");
051:                Pilot pilot1 = new Pilot("Michael Schumacher", 100);
052:                car1.setPilot(pilot1);
053:                db.set(car1);
054:            }
055:
056:            public static void storeSecondCar(ObjectContainer db) {
057:                Pilot pilot2 = new Pilot("Rubens Barrichello", 99);
058:                db.set(pilot2);
059:                Car car2 = new Car("BMW");
060:                car2.setPilot(pilot2);
061:                db.set(car2);
062:            }
063:
064:            public static void retrieveAllCarsQBE(ObjectContainer db) {
065:                Car proto = new Car(null);
066:                ObjectSet result = db.get(proto);
067:                listResult(result);
068:            }
069:
070:            public static void retrieveAllPilotsQBE(ObjectContainer db) {
071:                Pilot proto = new Pilot(null, 0);
072:                ObjectSet result = db.get(proto);
073:                listResult(result);
074:            }
075:
076:            public static void retrieveAllPilots(ObjectContainer db) {
077:                ObjectSet result = db.get(Pilot.class);
078:                listResult(result);
079:            }
080:
081:            public static void retrieveCarByPilotQBE(ObjectContainer db) {
082:                Pilot pilotproto = new Pilot("Rubens Barrichello", 0);
083:                Car carproto = new Car(null);
084:                carproto.setPilot(pilotproto);
085:                ObjectSet result = db.get(carproto);
086:                listResult(result);
087:            }
088:
089:            public static void retrieveCarByPilotNameQuery(ObjectContainer db) {
090:                Query query = db.query();
091:                query.constrain(Car.class);
092:                query.descend("pilot").descend("name").constrain(
093:                        "Rubens Barrichello");
094:                ObjectSet result = query.execute();
095:                listResult(result);
096:            }
097:
098:            public static void retrieveCarByPilotProtoQuery(ObjectContainer db) {
099:                Query query = db.query();
100:                query.constrain(Car.class);
101:                Pilot proto = new Pilot("Rubens Barrichello", 0);
102:                query.descend("pilot").constrain(proto);
103:                ObjectSet result = query.execute();
104:                listResult(result);
105:            }
106:
107:            public static void retrievePilotByCarModelQuery(ObjectContainer db) {
108:                Query carquery = db.query();
109:                carquery.constrain(Car.class);
110:                carquery.descend("model").constrain("Ferrari");
111:                Query pilotquery = carquery.descend("pilot");
112:                ObjectSet result = pilotquery.execute();
113:                listResult(result);
114:            }
115:
116:            public static void retrieveAllPilotsNative(ObjectContainer db) {
117:                ObjectSet results = db.query(new Predicate() {
118:                    public boolean match(Pilot pilot) {
119:                        return true;
120:                    }
121:                });
122:                listResult(results);
123:            }
124:
125:            public static void retrieveAllCars(ObjectContainer db) {
126:                ObjectSet results = db.get(Car.class);
127:                listResult(results);
128:            }
129:
130:            public static void retrieveCarsByPilotNameNative(ObjectContainer db) {
131:                final String pilotName = "Rubens Barrichello";
132:                ObjectSet results = db.query(new Predicate() {
133:                    public boolean match(Car car) {
134:                        return car.getPilot().getName().equals(pilotName);
135:                    }
136:                });
137:                listResult(results);
138:            }
139:
140:            public static void updateCar(ObjectContainer db) {
141:                ObjectSet result = db.query(new Predicate() {
142:                    public boolean match(Car car) {
143:                        return car.getModel().equals("Ferrari");
144:                    }
145:                });
146:                Car found = (Car) result.next();
147:                found.setPilot(new Pilot("Somebody else", 0));
148:                db.set(found);
149:                result = db.query(new Predicate() {
150:                    public boolean match(Car car) {
151:                        return car.getModel().equals("Ferrari");
152:                    }
153:                });
154:                listResult(result);
155:            }
156:
157:            public static void updatePilotSingleSession(ObjectContainer db) {
158:                ObjectSet result = db.query(new Predicate() {
159:                    public boolean match(Car car) {
160:                        return car.getModel().equals("Ferrari");
161:                    }
162:                });
163:                Car found = (Car) result.next();
164:                found.getPilot().addPoints(1);
165:                db.set(found);
166:                result = db.query(new Predicate() {
167:                    public boolean match(Car car) {
168:                        return car.getModel().equals("Ferrari");
169:                    }
170:                });
171:                listResult(result);
172:            }
173:
174:            public static void updatePilotSeparateSessionsPart1(
175:                    ObjectContainer db) {
176:                ObjectSet result = db.query(new Predicate() {
177:                    public boolean match(Car car) {
178:                        return car.getModel().equals("Ferrari");
179:                    }
180:                });
181:                Car found = (Car) result.next();
182:                found.getPilot().addPoints(1);
183:                db.set(found);
184:            }
185:
186:            public static void updatePilotSeparateSessionsPart2(
187:                    ObjectContainer db) {
188:                ObjectSet result = db.query(new Predicate() {
189:                    public boolean match(Car car) {
190:                        return car.getModel().equals("Ferrari");
191:                    }
192:                });
193:                listResult(result);
194:            }
195:
196:            public static void updatePilotSeparateSessionsImprovedPart1() {
197:                Db4o.configure().objectClass("com.db4o.f1.chapter2.Car")
198:                        .cascadeOnUpdate(true);
199:            }
200:
201:            public static void updatePilotSeparateSessionsImprovedPart2(
202:                    ObjectContainer db) {
203:                ObjectSet result = db.query(new Predicate() {
204:                    public boolean match(Car car) {
205:                        return car.getModel().equals("Ferrari");
206:                    }
207:                });
208:                Car found = (Car) result.next();
209:                found.getPilot().addPoints(1);
210:                db.set(found);
211:            }
212:
213:            public static void updatePilotSeparateSessionsImprovedPart3(
214:                    ObjectContainer db) {
215:                ObjectSet result = db.query(new Predicate() {
216:                    public boolean match(Car car) {
217:                        return car.getModel().equals("Ferrari");
218:                    }
219:                });
220:                listResult(result);
221:            }
222:
223:            public static void deleteFlat(ObjectContainer db) {
224:                ObjectSet result = db.query(new Predicate() {
225:                    public boolean match(Car car) {
226:                        return car.getModel().equals("Ferrari");
227:                    }
228:                });
229:                Car found = (Car) result.next();
230:                db.delete(found);
231:                result = db.get(new Car(null));
232:                listResult(result);
233:            }
234:
235:            public static void deleteDeepPart1() {
236:                Db4o.configure().objectClass("com.db4o.f1.chapter2.Car")
237:                        .cascadeOnDelete(true);
238:            }
239:
240:            public static void deleteDeepPart2(ObjectContainer db) {
241:                ObjectSet result = db.query(new Predicate() {
242:                    public boolean match(Car car) {
243:                        return car.getModel().equals("BMW");
244:                    }
245:                });
246:                Car found = (Car) result.next();
247:                db.delete(found);
248:                result = db.query(new Predicate() {
249:                    public boolean match(Car car) {
250:                        return true;
251:                    }
252:                });
253:                listResult(result);
254:            }
255:
256:            public static void deleteDeepRevisited(ObjectContainer db) {
257:                ObjectSet result = db.query(new Predicate() {
258:                    public boolean match(Pilot pilot) {
259:                        return pilot.getName().equals("Michael Schumacher");
260:                    }
261:                });
262:                if (!result.hasNext()) {
263:                    System.out.println("Pilot not found!");
264:                    return;
265:                }
266:                Pilot pilot = (Pilot) result.next();
267:                Car car1 = new Car("Ferrari");
268:                Car car2 = new Car("BMW");
269:                car1.setPilot(pilot);
270:                car2.setPilot(pilot);
271:                db.set(car1);
272:                db.set(car2);
273:                db.delete(car2);
274:                result = db.query(new Predicate() {
275:                    public boolean match(Car car) {
276:                        return true;
277:                    }
278:                });
279:                listResult(result);
280:            }
281:
282:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.