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: }
|