PageRenderTime 35ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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