PageRenderTime 24ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/test/OwnedJoinsJPA.java

http://datanucleus-appengine.googlecode.com/
Java | 223 lines | 160 code | 45 blank | 18 comment | 33 complexity | 8065db3ae229c41d227cde85df0ca43a MD5 | raw file
  1/**********************************************************************
  2Copyright (c) 2009 Google Inc.
  3
  4Licensed under the Apache License, Version 2.0 (the "License");
  5you may not use this file except in compliance with the License.
  6You may obtain a copy of the License at
  7
  8http://www.apache.org/licenses/LICENSE-2.0
  9
 10Unless required by applicable law or agreed to in writing, software
 11distributed under the License is distributed on an "AS IS" BASIS,
 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13See the License for the specific language governing permissions and
 14limitations under the License.
 15**********************************************************************/
 16package com.google.appengine.datanucleus.test;
 17
 18import com.google.appengine.api.datastore.Key;
 19
 20import java.util.ArrayList;
 21import java.util.Arrays;
 22import java.util.List;
 23
 24import javax.persistence.CascadeType;
 25import javax.persistence.Entity;
 26import javax.persistence.FetchType;
 27import javax.persistence.GeneratedValue;
 28import javax.persistence.GenerationType;
 29import javax.persistence.Id;
 30import javax.persistence.OneToMany;
 31import javax.persistence.OneToOne;
 32
 33/**
 34 * @author Max Ross <maxr@google.com>
 35 */
 36public class OwnedJoinsJPA {
 37
 38  @Entity
 39  public static class Student {
 40    @Id
 41    @GeneratedValue(strategy = GenerationType.IDENTITY)
 42    private Long id;
 43
 44    private int grade;
 45
 46    @OneToMany(cascade = CascadeType.ALL)
 47    private List<Course> courses = new ArrayList<Course>();
 48
 49    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
 50    private Major major;
 51
 52    public int getGrade() {
 53      return grade;
 54    }
 55
 56    public void setGrade(int grade) {
 57      this.grade = grade;
 58    }
 59
 60    public List<Course> getCourses() {
 61      return courses;
 62    }
 63
 64    public void setCourses(List<Course> courses) {
 65      this.courses = courses;
 66    }
 67
 68    public Major getMajor() {
 69      return major;
 70    }
 71
 72    public void setMajor(Major major) {
 73      this.major = major;
 74    }
 75
 76    public Long getId() {
 77      return id;
 78    }
 79
 80    @Override
 81    public boolean equals(Object o) {
 82      if (this == o) {
 83        return true;
 84      }
 85      if (o == null || getClass() != o.getClass()) {
 86        return false;
 87      }
 88
 89      Student student = (Student) o;
 90
 91      if (id != null ? !id.equals(student.id) : student.id != null) {
 92        return false;
 93      }
 94
 95      return true;
 96    }
 97
 98    @Override
 99    public int hashCode() {
100      return id != null ? id.hashCode() : 0;
101    }
102  }
103
104  @Entity
105  public static class Course {
106    @Id
107    @GeneratedValue(strategy = GenerationType.IDENTITY)
108    private Key key;
109
110    private String department;
111
112    public Key getKey() {
113      return key;
114    }
115
116    public String getDepartment() {
117      return department;
118    }
119
120    public void setDepartment(String department) {
121      this.department = department;
122    }
123
124    @Override
125    public boolean equals(Object o) {
126      if (this == o) {
127        return true;
128      }
129      if (o == null || getClass() != o.getClass()) {
130        return false;
131      }
132
133      Course course = (Course) o;
134
135      if (key != null ? !key.equals(course.key) : course.key != null) {
136        return false;
137      }
138
139      return true;
140    }
141
142    @Override
143    public int hashCode() {
144      return key != null ? key.hashCode() : 0;
145    }
146  }
147
148  @Entity
149  public static class Major {
150    @Id
151    @GeneratedValue(strategy = GenerationType.IDENTITY)
152    private Key key;
153
154    private String school;
155
156    public Key getKey() {
157      return key;
158    }
159
160    public void setKey(Key key) {
161      this.key = key;
162    }
163
164    public String getSchool() {
165      return school;
166    }
167
168    public void setSchool(String school) {
169      this.school = school;
170    }
171
172    @Override
173    public boolean equals(Object o) {
174      if (this == o) {
175        return true;
176      }
177      if (o == null || getClass() != o.getClass()) {
178        return false;
179      }
180
181      Major major = (Major) o;
182
183      if (key != null ? !key.equals(major.key) : major.key != null) {
184        return false;
185      }
186
187      return true;
188    }
189
190    @Override
191    public int hashCode() {
192      int result = key != null ? key.hashCode() : 0;
193      result = 31 * result + (school != null ? school.hashCode() : 0);
194      return result;
195    }
196  }
197
198  public static Student newStudent(int grade, Course... courses) {
199    return newStudent(grade, null, courses);
200  }
201
202  public static Student newStudent(int grade, Major major, Course... courses) {
203    Student s = new Student();
204    s.setGrade(grade);
205    s.setCourses(Arrays.asList(courses));
206    if (major != null) {
207      s.setMajor(major);
208    }
209    return s;
210  }
211
212  public static Course newCourse(String dept) {
213    Course c = new Course();
214    c.setDepartment(dept);
215    return c;
216  }
217
218  public static Major newMajor(String school) {
219    Major m = new Major();
220    m.setSchool(school);
221    return m;
222  }
223}