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


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements.  See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership.  The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License.  You may obtain a copy of the License at
009:         *
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied.  See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.    
018:         */
019:        package relations;
020:
021:        import java.util.*;
022:        import javax.persistence.*;
023:
024:        // import the enums for MALE and FEMALE
025:        import static relations.Deity.Gender.*;
026:
027:        /** 
028:         * A very simple, stand-alone program that stores a new entity in the
029:         * database and then performs a query to retrieve it.
030:         */
031:        public class Main {
032:
033:            @SuppressWarnings("unchecked")
034:            public static void main(String[] args) {
035:                // Create a new EntityManagerFactory using the System properties.
036:                // The "relations" name will be used to configure based on the
037:                // corresponding name in the META-INF/persistence.xml file
038:                EntityManagerFactory factory = Persistence
039:                        .createEntityManagerFactory("relations", System
040:                                .getProperties());
041:
042:                // Create a new EntityManager from the EntityManagerFactory. The
043:                // EntityManager is the main object in the persistence API, and is
044:                // used to create, delete, and query objects, as well as access
045:                // the current transaction
046:                EntityManager em = factory.createEntityManager();
047:
048:                initFamilyTree(em);
049:
050:                runQueries(em);
051:
052:                // It is always good to clean up after ourselves
053:                em.close();
054:                factory.close();
055:            }
056:
057:            /** 
058:             * Creates a partial family tree of the Greek dieties.
059:             *  
060:             * @param  em  the EntityManager to use in the persistence process
061:             */
062:            public static void initFamilyTree(EntityManager em) {
063:
064:                // First delete all the members from the database the clean up
065:                em.getTransaction().begin();
066:                em.createQuery("delete from Deity").executeUpdate();
067:                em.getTransaction().commit();
068:
069:                // Generation 1
070:                Deity uranus = new Deity("Uranus", MALE);
071:                Deity gaea = new Deity("Gaea", FEMALE);
072:
073:                // Generation 2
074:                Deity cronus = gaea.giveBirth("Cronus", uranus, MALE);
075:                Deity rhea = gaea.giveBirth("Rhea", uranus, FEMALE);
076:                Deity coeus = gaea.giveBirth("Coeus", uranus, MALE);
077:                Deity phoebe = gaea.giveBirth("Phoebe", uranus, FEMALE);
078:                Deity oceanus = gaea.giveBirth("Oceanus", uranus, MALE);
079:                Deity tethys = gaea.giveBirth("Tethys", uranus, FEMALE);
080:
081:                // Generation 3
082:                Deity leto = phoebe.giveBirth("Leto", coeus, FEMALE);
083:
084:                Deity hestia = rhea.giveBirth("Hestia", cronus, FEMALE);
085:                Deity pluto = rhea.giveBirth("Pluto", cronus, MALE);
086:                Deity poseidon = rhea.giveBirth("Poseidon", cronus, MALE);
087:                Deity zeus = rhea.giveBirth("Zeus", cronus, MALE);
088:                Deity hera = rhea.giveBirth("Hera", cronus, FEMALE);
089:                Deity demeter = rhea.giveBirth("Demeter", cronus, FEMALE);
090:
091:                // Generation 4
092:                Deity iapetus = tethys.giveBirth("Iapetus", coeus, MALE);
093:                Deity clymene = new Deity("Clymene", FEMALE);
094:
095:                Deity apollo = leto.giveBirth("Apollo", zeus, MALE);
096:                Deity artemis = leto.giveBirth("Artemis", zeus, MALE);
097:
098:                Deity persephone = demeter.giveBirth("Persephone", zeus, MALE);
099:
100:                Deity ares = hera.giveBirth("Ares", zeus, MALE);
101:                Deity hebe = hera.giveBirth("Hebe", zeus, FEMALE);
102:                Deity hephaestus = hera.giveBirth("Hephaestus", zeus, MALE);
103:
104:                Deity prometheus = clymene.giveBirth("Prometheus", iapetus,
105:                        MALE);
106:                Deity atlas = clymene.giveBirth("Atlas", iapetus, MALE);
107:                Deity epimetheus = clymene.giveBirth("Epimetheus", iapetus,
108:                        FEMALE);
109:
110:                Deity dione = new Deity("Dione", FEMALE);
111:                dione.giveBirth("Aphrodite", zeus, FEMALE);
112:
113:                // Begin a new local transaction so that we can persist a new entity
114:                em.getTransaction().begin();
115:
116:                // note that we only need to explicitly persist a single root of the
117:                // object graph (the family tree, in this case), since we have the
118:                // "cascade" annotation on all the relations
119:                em.persist(zeus);
120:
121:                // Commit the transaction, which will cause the entity to
122:                // be stored in the database
123:                em.getTransaction().commit();
124:            }
125:
126:            /** 
127:             * Run some sample queries against the family tree model.
128:             *  
129:             * @param  em  the EntityManager to use
130:             */
131:            public static void runQueries(EntityManager em) {
132:
133:                System.out.println("Running query to find all instances..");
134:
135:                // Perform a simple query for all the Deity entities
136:                Query q = em.createQuery("select x from Deity x");
137:
138:                // Go through each of the entities and print out each of their
139:                // messages, as well as the date on which it was created 
140:                for (Deity m : (List<Deity>) q.getResultList()) {
141:                    System.out.println(m.getName());
142:                }
143:
144:                q = em.createQuery("select x from Deity x "
145:                        + "where x.father.name = 'Zeus'");
146:
147:                for (Deity m : (List<Deity>) q.getResultList()) {
148:                    System.out.println("Child of Zeus: " + m.getName());
149:                }
150:
151:                q = em.createNamedQuery("siblings").setParameter(1,
152:                        em.getReference(Deity.class, "Rhea"));
153:
154:                for (Deity m : (List<Deity>) em.createNamedQuery("siblings")
155:                        .setParameter(1, em.getReference(Deity.class, "Rhea"))
156:                        .getResultList()) {
157:                    System.out.println("Siblings of Rhea: " + m.getName());
158:                }
159:
160:                for (Deity m : (List<Deity>) em.createNamedQuery(
161:                        "half-siblings").setParameter(1,
162:                        em.getReference(Deity.class, "Apollo")).getResultList()) {
163:                    System.out.println("Half-siblings of Apollo: "
164:                            + m.getName());
165:                }
166:
167:                for (Deity m : (List<Deity>) em.createNamedQuery("cousins")
168:                        .setParameter(1, em.getReference(Deity.class, "Leto"))
169:                        .getResultList()) {
170:                    System.out.println("Cousins of Leto: " + m.getName());
171:                }
172:            }
173:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.