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.io.Serializable;
022: import java.util.HashSet;
023: import java.util.Set;
024:
025: import javax.persistence.Basic;
026: import javax.persistence.CascadeType;
027: import javax.persistence.Entity;
028: import javax.persistence.Enumerated;
029: import javax.persistence.EnumType;
030: import javax.persistence.Id;
031: import javax.persistence.NamedQuery;
032: import javax.persistence.NamedQueries;
033: import javax.persistence.OneToMany;
034: import javax.persistence.OneToOne;
035:
036: /**
037: * An entity that contains relations corresponding to family tree relations.
038: * This entity demonstrates the following JPA features:
039: *
040: * 1. Enum fields (gender)
041: * 2. @OneToOne relations
042: * 3. @OneToMany relations
043: * 4. Named queries
044: */
045: @Entity
046: @NamedQueries({@NamedQuery(name="siblings",query="select distinct sibling1 " + "from Deity sibling1, Deity sibling2 where " + "sibling1.father = sibling2.father "+ "and sibling1.mother = sibling2.mother "+ "and sibling2 = ?1 and sibling1 <> ?1"),@NamedQuery(name="half-siblings",query="select distinct sibling1 " + "from Deity sibling1, Deity sibling2 where " + "((sibling1.father = sibling2.father "+ "and sibling1.mother <> sibling2.mother) "+ "or (sibling1.father <> sibling2.father "+ "and sibling1.mother = sibling2.mother)) "+ "and sibling2 = ?1 and sibling1 <> ?1"),@NamedQuery(name="cousins",query="select distinct cousin1 " + "from Deity cousin1, Deity cousin2 where " + "("+ "cousin1.father.father = cousin2.father.father "+ "or cousin1.father.mother = cousin2.father.mother "+ "or cousin1.mother.father = cousin2.mother.father "+ "or cousin1.mother.mother = cousin2.mother.mother) "+ "and (cousin1.father <> cousin2.father) "+ "and (cousin1.mother <> cousin2.mother) "+ "and cousin2 = ?1 and cousin1 <> ?1")})
047: public class Deity implements Serializable {
048: // the Id is the name, which is generally a bad idea, but we are
049: // confident that diety names will be unique
050: @Id
051: private String name;
052:
053: @Basic
054: @Enumerated(EnumType.STRING)
055: private Gender gender;
056:
057: @OneToOne(cascade=CascadeType.ALL)
058: private Deity mother;
059:
060: @OneToOne(cascade=CascadeType.ALL)
061: private Deity father;
062:
063: @OneToMany(cascade=CascadeType.ALL)
064: private Set<Deity> children;
065:
066: public static enum Gender {
067: MALE, FEMALE
068: }
069:
070: public Deity(String name, Gender gender) {
071: this .name = name;
072: this .gender = gender;
073: }
074:
075: //////////////////////////
076: // Business methods follow
077: //////////////////////////
078:
079: /**
080: * She's having a baby...
081: *
082: * @param childName the baby name
083: * @return the new child
084: *
085: * @throws IllegalArgumentException if the person is not a woman, or
086: * if the person is unmarried (illegitimate
087: * children are not yet supported)
088: */
089: public Deity giveBirth(String childName, Deity childFather,
090: Gender gender) {
091: if (this .gender != Gender.FEMALE)
092: throw new IllegalArgumentException(
093: "Only women can have children!");
094:
095: if (childName == null)
096: throw new IllegalArgumentException("No child name!");
097:
098: // create the child
099: Deity child = new Deity(childName, gender);
100:
101: // set the parents in the children...
102: child.mother = this ;
103:
104: // add the child to this member's children
105: if (children == null)
106: children = new HashSet<Deity>();
107: children.add(child);
108:
109: if (childFather != null) {
110: child.father = childFather;
111: if (childFather.children == null)
112: childFather.children = new HashSet<Deity>();
113: childFather.children.add(child);
114: }
115:
116: return child;
117: }
118:
119: ////////////////////////////////////
120: // Property accessor methods follow
121: ////////////////////////////////////
122:
123: public void setName(String name) {
124: this .name = name;
125: }
126:
127: public String getName() {
128: return this .name;
129: }
130:
131: public void setGender(Gender gender) {
132: this .gender = gender;
133: }
134:
135: public Gender getGender() {
136: return this .gender;
137: }
138:
139: public void setMother(Deity mother) {
140: this .mother = mother;
141: }
142:
143: public Deity getMother() {
144: return this .mother;
145: }
146:
147: public void setFather(Deity father) {
148: this .father = father;
149: }
150:
151: public Deity getFather() {
152: return this .father;
153: }
154:
155: public void setChildren(Set<Deity> children) {
156: this .children = children;
157: }
158:
159: public Set<Deity> getChildren() {
160: return this.children;
161: }
162:
163: }
|