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 org.apache.openjpa.persistence.kernel.common.apps;
020:
021: import java.util.HashSet;
022: import java.util.Set;
023: import javax.persistence.CascadeType;
024: import javax.persistence.Entity;
025: import javax.persistence.FetchType;
026: import javax.persistence.ManyToMany;
027: import javax.persistence.ManyToOne;
028:
029: import org.apache.openjpa.persistence.FetchAttribute;
030: import org.apache.openjpa.persistence.FetchGroup;
031: import org.apache.openjpa.persistence.FetchGroups;
032:
033: /**
034: * @author <A HREF="mailto:pinaki.poddar@gmail.com>Pinaki Poddar</A>
035: */
036: @Entity
037: @FetchGroups({@FetchGroup(name="detail+children-names",fetchGroups="detail"),@FetchGroup(name="detail",fetchGroups="default",attributes=@FetchAttribute(name="address")),@FetchGroup(name="detail+children-list",fetchGroups="detail",attributes=@FetchAttribute(name="children")),@FetchGroup(name="person.address",attributes=@FetchAttribute(name="address"))})
038: public class PCPerson {
039:
040: private String name;
041:
042: @ManyToOne(fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
043: private PCAddress address;
044:
045: @ManyToOne(fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
046: private PCPerson parent;
047:
048: @ManyToMany(cascade=CascadeType.PERSIST)
049: private Set<PCPerson> children;
050:
051: protected PCPerson() {
052: super ();
053: }
054:
055: public PCPerson(String name) {
056: setName(name);
057: }
058:
059: public PCAddress getAddress() {
060: return address;
061: }
062:
063: public void setAddress(PCAddress address) {
064: this .address = address;
065: }
066:
067: public Set getChildren() {
068: return children;
069: }
070:
071: public void setChildren(Set children) {
072: this .children = children;
073: }
074:
075: public void addChildren(PCPerson child) {
076: if (children == null)
077: children = new HashSet();
078: children.add(child);
079: }
080:
081: public String getName() {
082: return name;
083: }
084:
085: public void setName(String name) {
086: this .name = name;
087: }
088:
089: public PCPerson getParent() {
090: return parent;
091: }
092:
093: public void setParent(PCPerson parent) {
094: this .parent = parent;
095: }
096:
097: public static Object reflect(PCPerson instance, String name) {
098: if (instance == null)
099: return null;
100: try {
101: return PCPerson.class.getDeclaredField(name).get(instance);
102: } catch (Exception e) {
103: throw new RuntimeException(e);
104: }
105: }
106: }
|