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.GeneratedValue;
027: import javax.persistence.GenerationType;
028: import javax.persistence.Id;
029: import javax.persistence.ManyToMany;
030: import javax.persistence.ManyToOne;
031:
032: import org.apache.openjpa.persistence.FetchAttribute;
033: import org.apache.openjpa.persistence.FetchGroup;
034: import org.apache.openjpa.persistence.FetchGroups;
035:
036: /**
037: * @author <A HREF="mailto:pinaki.poddar@gmail.com>Pinaki Poddar</A>
038: */
039: @Entity
040: @FetchGroups({@FetchGroup(name="company.address",attributes=@FetchAttribute(name="address")),@FetchGroup(name="company.departments",attributes=@FetchAttribute(name="departments")),@FetchGroup(name="default",postLoad=false,attributes=@FetchAttribute(name="name"))})
041: public class PCCompany {
042:
043: @Id
044: @GeneratedValue(strategy=GenerationType.AUTO)
045: private int id;
046:
047: private String name;
048:
049: @ManyToOne(fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
050: private PCAddress address;
051:
052: @ManyToMany(cascade=CascadeType.PERSIST)
053: private Set<PCDepartment> departments;
054:
055: public PCCompany() {
056: super ();
057: }
058:
059: public PCCompany(String name) {
060: super ();
061: setName(name);
062: }
063:
064: public int getId() {
065: return this .id;
066: }
067:
068: public PCAddress getAddress() {
069: return address;
070: }
071:
072: public void setAddress(PCAddress address) {
073: this .address = address;
074: }
075:
076: public String getName() {
077: return name;
078: }
079:
080: public void setName(String name) {
081: this .name = name;
082: }
083:
084: public Set getDepartments() {
085: return departments;
086: }
087:
088: public void setDepartments(Set departments) {
089: this .departments = departments;
090: }
091:
092: public void addDepartment(PCDepartment dept) {
093: if (departments == null)
094: departments = new HashSet();
095: departments.add(dept);
096: dept.setCompany(this );
097: }
098:
099: public boolean contains(PCDepartment dept) {
100: return departments != null && departments.contains(dept);
101: }
102:
103: public static Object reflect(PCCompany instance, String name) {
104: if (instance == null)
105: return null;
106: try {
107: return PCCompany.class.getDeclaredField(name).get(instance);
108: } catch (Exception e) {
109: throw new RuntimeException(e);
110: }
111: }
112: }
|