Source Code Cross Referenced for Test.java in  » Database-ORM » castor » jdo » 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 ORM » castor » jdo 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package jdo;
002:
003:        import java.io.IOException;
004:        import java.io.PrintWriter;
005:        import java.sql.Connection;
006:        import java.sql.Statement;
007:
008:        import myapp.*;
009:
010:        import org.apache.commons.logging.Log;
011:        import org.apache.commons.logging.LogFactory;
012:
013:        import org.exolab.castor.jdo.Database;
014:        import org.exolab.castor.jdo.JDOManager;
015:        import org.exolab.castor.jdo.OQLQuery;
016:        import org.exolab.castor.jdo.QueryResults;
017:        import org.exolab.castor.mapping.Mapping;
018:        import org.exolab.castor.mapping.MappingException;
019:        import org.exolab.castor.mapping.xml.ClassMapping;
020:        import org.exolab.castor.mapping.xml.FieldMapping;
021:        import org.exolab.castor.mapping.xml.MappingRoot;
022:        import org.exolab.castor.xml.Marshaller;
023:
024:        /**
025:         * This example is only intended to show how castor can be set up 
026:         * in a standalone environment. For detailed examples on the mapping file, 
027:         * database schemas, supported features and their expected behaviors, 
028:         * please consult the JDO test cases instead. The JDO test cases can be 
029:         * found in the full CVS snapshot and located under the directory of 
030:         * src/tests/jdo and src/tests/myapp.
031:         */
032:        public class Test {
033:            private static final String DATABASE_NAME = "test";
034:
035:            private static Log LOG = LogFactory.getLog(Test.class);
036:
037:            public static final String JDO_CONFIG_FILE = "jdo-conf.xml";
038:
039:            public static final String MAPPING_FILE = "mapping.xml";
040:
041:            public static final String USAGE_INSTRUCTIONS = "Usage: example jdo";
042:
043:            private Mapping _mapping;
044:
045:            private JDOManager _jdo;
046:
047:            public static void main(String[] args) {
048:                try {
049:                    Test test = new Test();
050:                    test.setup();
051:                    test.run();
052:                } catch (Exception except) {
053:                    LOG.error(except, except);
054:                }
055:            }
056:
057:            public Test() throws IOException, MappingException {
058:
059:                // Load the mapping file
060:                _mapping = new Mapping(getClass().getClassLoader());
061:                _mapping.loadMapping(getClass().getResource(MAPPING_FILE));
062:
063:                String jdoConf = getClass().getResource(JDO_CONFIG_FILE)
064:                        .toString();
065:                JDOManager.loadConfiguration(jdoConf);
066:                _jdo = JDOManager.createInstance(DATABASE_NAME);
067:            }
068:
069:            public void setup() throws Exception {
070:                Database db = _jdo.getDatabase();
071:                db.begin();
072:                Connection connection = db.getJdbcConnection();
073:                Statement statement = connection.createStatement();
074:                statement
075:                        .executeUpdate("create table prod (id int not null, name varchar(200) not null, price numeric(18,2) not null,  group_id int not null)");
076:                statement
077:                        .executeUpdate("create table prod_group (id int not null, name varchar(200) not null)");
078:                statement
079:                        .executeUpdate("create table prod_detail (id int not null, prod_id int not null, name varchar(200) not null)");
080:                statement
081:                        .executeUpdate("create table computer (id int not null, cpu varchar(200) not null)");
082:                statement
083:                        .executeUpdate("create table category (id int not null, name varchar(200) not null)");
084:                statement
085:                        .executeUpdate("create table category_prod (prod_id int not null, category_id int not null)");
086:                db.commit();
087:                db.close();
088:            }
089:
090:            public void run() throws Exception {
091:                Database db;
092:                Product product = null;
093:                ProductGroup group;
094:                Category category;
095:                ProductDetail detail;
096:                Computer computer = null;
097:                OQLQuery productOql;
098:                OQLQuery groupOql;
099:                OQLQuery categoryOql;
100:                OQLQuery computerOql;
101:                QueryResults results;
102:
103:                db = _jdo.getDatabase();
104:
105:                db.begin();
106:                LOG.info("Begin transaction to remove Product objects");
107:
108:                // Look up the products and if found in the database,
109:                // delete them from the database
110:                productOql = db
111:                        .getOQLQuery("SELECT p FROM myapp.Product p WHERE p.id = $1");
112:
113:                for (int i = 4; i < 10; ++i) {
114:                    LOG.debug("Executing OQL");
115:                    productOql.bind(i);
116:                    results = productOql.execute();
117:
118:                    while (results.hasMore()) {
119:                        product = (Product) results.next();
120:                        LOG.debug("Deleting existing product: " + product);
121:                        db.remove(product);
122:                    }
123:                }
124:
125:                LOG.info("End transaction to remove Product objects");
126:                db.commit();
127:
128:                db.begin();
129:                LOG.info("Begin transaction to remove Computer object");
130:
131:                // Look up the computer and if found in the database,
132:                // delete ethis object from the database
133:                computerOql = db
134:                        .getOQLQuery("SELECT c FROM myapp.Computer c WHERE c.id = $1");
135:                computerOql.bind(44);
136:                results = computerOql.execute();
137:
138:                while (results.hasMore()) {
139:                    computer = (Computer) results.next();
140:                    LOG.debug("Deleting existing computer: " + computer);
141:                    db.remove(computer);
142:                }
143:
144:                LOG.info("End transaction to remove Computer objects");
145:                db.commit();
146:
147:                db.begin();
148:                LOG.info("Begin transaction to remove Category objects");
149:
150:                // Look up the categories and if found in the database,
151:                // delete this object from the database
152:                categoryOql = db
153:                        .getOQLQuery("SELECT c FROM myapp.Category c WHERE c.id = $1");
154:
155:                // Still debugging this area because deletion of Category objects is not 
156:                // working the second time around
157:                for (int i = 7; i < 10; ++i) {
158:                    categoryOql.bind(i);
159:                    results = categoryOql.execute();
160:                    while (results.hasMore()) {
161:                        category = (Category) results.next();
162:                        LOG.debug("Deleting existing category: " + category);
163:                        db.remove(category);
164:                    }
165:                }
166:
167:                LOG.info("End transaction to remove Category objects");
168:                db.commit();
169:
170:                db.begin();
171:                LOG
172:                        .info("Begin transaction: one-to-one, one-to-many, and dependent relations");
173:                // If no such group exists in the database, create a new
174:                // object and persist it
175:                groupOql = db
176:                        .getOQLQuery("SELECT g FROM myapp.ProductGroup g WHERE id = $1");
177:                groupOql.bind(3);
178:                results = groupOql.execute();
179:
180:                if (!results.hasMore()) {
181:                    group = new ProductGroup();
182:                    group.setId(3);
183:                    group.setName("a group");
184:                    db.create(group);
185:                    LOG.debug("Creating new group: " + group);
186:                } else {
187:                    group = (ProductGroup) results.next();
188:                    LOG.debug("Query result: " + group);
189:                }
190:
191:                // If no such product exists in the database, create a new
192:                // object and persist it
193:                // Note: product uses group, so group object has to be
194:                //       created first, but can be persisted later
195:                productOql.bind(4);
196:                results = productOql.execute();
197:
198:                if (!results.hasMore()) {
199:                    product = new Product();
200:                    product.setId(4);
201:                    product.setName("product4");
202:                    product.setPrice(200);
203:                    product.setGroup(group);
204:                    detail = new ProductDetail();
205:                    detail.setId(1);
206:                    detail.setName("keyboard");
207:                    product.addDetail(detail);
208:                    detail = new ProductDetail();
209:                    detail.setId(2);
210:                    detail.setName("mouse");
211:                    product.addDetail(detail);
212:                    detail = new ProductDetail();
213:                    detail.setId(3);
214:                    detail.setName("monitor");
215:                    product.addDetail(detail);
216:                    LOG.debug("Creating new product: " + product);
217:                    db.create(product);
218:                } else {
219:                    LOG.debug("Query result: " + results.next());
220:                }
221:
222:                // If no such computer exists in the database, create a new
223:                // object and persist it
224:                // Note: computer uses group, so group object has to be
225:                //       created first, but can be persisted later
226:                computerOql.bind(44);
227:                results = computerOql.execute();
228:
229:                if (!results.hasMore()) {
230:                    computer = new Computer();
231:                    computer.setId(44);
232:                    computer.setCpu("Pentium");
233:                    computer.setName("MyPC");
234:                    computer.setPrice(400);
235:                    computer.setGroup(group);
236:                    detail = new ProductDetail();
237:                    detail.setId(4);
238:                    detail.setName("network card");
239:                    computer.addDetail(detail);
240:                    detail = new ProductDetail();
241:                    detail.setId(5);
242:                    detail.setName("scsi card");
243:                    computer.addDetail(detail);
244:                    LOG.debug("Creating new computer: " + computer);
245:                    db.create(computer);
246:                } else {
247:                    LOG.debug("Query result: " + results.next());
248:                }
249:                LOG
250:                        .info("End transaction: one-to-one, one-to-many and dependent relations");
251:                db.commit();
252:
253:                // Many-to-many example using one existing product
254:                db.begin();
255:                LOG
256:                        .info("Begin transaction: one-to-one and dependent relations");
257:
258:                // If no such products with ids 5-8 exist, create new
259:                // objects and persist them
260:                for (int i = 5; i < 10; ++i) {
261:                    int j = i + 1;
262:                    productOql.bind(j);
263:                    results = productOql.execute();
264:
265:                    if (!results.hasMore()) {
266:                        product = new Product();
267:                        product.setId(i);
268:                        product.setName("product" + product.getId());
269:                        product.setPrice(300);
270:                        product.setGroup(group);
271:                        detail = new ProductDetail();
272:                        detail.setId(j);
273:                        detail.setName("detail" + detail.getId());
274:                        product.addDetail(detail);
275:                        LOG.debug("Creating new product: " + product);
276:                        db.create(product);
277:                    } else {
278:                        LOG.debug("Query result: " + results.next());
279:                    }
280:                }
281:
282:                LOG
283:                        .info("End transaction: one-to-one and dependent relations ");
284:                db.commit();
285:
286:                db.begin();
287:                LOG.info("Begin transaction: many-to-many relations");
288:
289:                for (int x = 4; x < 7; ++x) {
290:                    int y = x + 3;
291:                    product = (Product) db.load(Product.class, new Integer(y));
292:
293:                    // If no such categories exists in the database, create new
294:                    // objects and persist them
295:                    categoryOql.bind(x);
296:                    results = categoryOql.execute();
297:
298:                    if (!results.hasMore()) {
299:                        category = new Category();
300:                        category.setId(x);
301:                        category.setName("category" + category.getId());
302:                        category.addProduct(product);
303:                        db.create(category);
304:                        LOG.debug("Creating new category: " + category);
305:                    } else {
306:                        category = (Category) results.next();
307:                        LOG.debug("Query result: " + category);
308:                    }
309:                }
310:
311:                LOG.info("End transaction: many-to-many relations");
312:                db.commit();
313:
314:                product.setPrice(333);
315:                LOG.info("Updated Product price: " + product);
316:
317:                db.begin();
318:                LOG.info("Begin transaction: long transaction");
319:
320:                //
321:                // Don't forget to implement TimeStampable for the long transaction!!!
322:                //
323:                db.update(product);
324:                LOG.info("End transaction: long transaction");
325:                db.commit();
326:
327:                db.begin();
328:                LOG
329:                        .info("Begin transaction: update extends relation in long transaction ");
330:
331:                computerOql.bind(44);
332:                results = computerOql.execute();
333:
334:                while (results.hasMore()) {
335:                    computer = new Computer();
336:                    computer = (Computer) results.next();
337:                    LOG.debug("Found existing computer: " + computer);
338:                }
339:
340:                LOG
341:                        .info("End transaction: update extends relation in long transaction");
342:                db.commit();
343:
344:                computer.setPrice(425);
345:                LOG.info("Updated Computer price: " + product);
346:
347:                db.begin();
348:                LOG
349:                        .info("Begin transaction: update extends relation in long transaction ");
350:
351:                //
352:                // Don't forget to implement TimeStampable for the long transaction!!!
353:                //
354:                db.update(computer);
355:
356:                LOG
357:                        .info("End transaction: update extends relation in long transaction");
358:                db.commit();
359:
360:                db.begin();
361:                LOG
362:                        .info("Begin transaction: simple load and update within the same transaction");
363:
364:                computerOql.bind(44);
365:                results = computerOql.execute();
366:
367:                if (results.hasMore()) {
368:                    computer = (Computer) results.next();
369:                    computer.setCpu("Opteron");
370:                }
371:
372:                LOG
373:                        .info("End transaction: simple load and update within the same transaction");
374:                db.commit();
375:
376:                db.begin();
377:                LOG
378:                        .info("Begin transaction: simple load to test the previous change");
379:
380:                computerOql.bind(44);
381:                results = computerOql.execute();
382:
383:                if (results.hasMore()) {
384:                    LOG.info("Loaded computer:" + results.next());
385:                }
386:
387:                LOG
388:                        .info("End transaction: simple load to test the previous change");
389:                db.commit();
390:
391:                Marshaller marshaller;
392:
393:                marshaller = new Marshaller(new PrintWriter(System.out));
394:                marshaller.setMapping(_mapping);
395:
396:                db.begin();
397:                LOG.info("Begin transaction: marshalling objects to XML");
398:
399:                computerOql = db
400:                        .getOQLQuery("SELECT c FROM myapp.Computer c WHERE c.id >= $1");
401:                computerOql.bind(10);
402:                results = computerOql.execute();
403:
404:                while (results.hasMore())
405:                    marshaller.marshal(results.next());
406:
407:                LOG.info("End transaction: marshalling objects to XML");
408:                db.commit();
409:
410:                db.close();
411:                LOG.info("Test complete");
412:
413:                // --------------------------------------------------------------------
414:
415:                LOG.info("Begin: Walking the mapping descriptor via objects");
416:
417:                MappingRoot mappingRoot = _mapping.getRoot();
418:                ClassMapping classMap;
419:                FieldMapping fieldMap;
420:                FieldMapping[] fieldMaps;
421:
422:                int mappingCount = mappingRoot.getClassMappingCount();
423:
424:                // loop over the classes
425:                for (int i = 0; i < mappingCount; ++i) {
426:                    classMap = mappingRoot.getClassMapping(i);
427:                    LOG.debug("Class name: " + classMap.getName());
428:
429:                    fieldMaps = classMap.getClassChoice().getFieldMapping();
430:
431:                    LOG.debug("fieldMaps.length: " + fieldMaps.length);
432:
433:                    // loop over the fields in each class
434:                    for (int j = 0; j < fieldMaps.length; ++j) {
435:                        fieldMap = fieldMaps[j];
436:                        LOG.debug("        Field name: " + fieldMap.getName());
437:                        LOG.debug("        Field type: " + fieldMap.getType());
438:                    }
439:                }
440:
441:                LOG.info("End: Walking the mapping descriptor via objects");
442:
443:                // --------------------------------------------------------------------
444:            }
445:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.