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

http://datanucleus-appengine.googlecode.com/ · Java · 221 lines · 158 code · 45 blank · 18 comment · 34 complexity · 85e943045804a98d53506f59215c1981 MD5 · raw file

  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.List;
  18. import javax.jdo.annotations.Element;
  19. import javax.jdo.annotations.IdGeneratorStrategy;
  20. import javax.jdo.annotations.PersistenceCapable;
  21. import javax.jdo.annotations.Persistent;
  22. import javax.jdo.annotations.PrimaryKey;
  23. /**
  24. * @author Max Ross <maxr@google.com>
  25. */
  26. public class UnownedJoinsJDO {
  27. @PersistenceCapable(detachable = "true")
  28. public static class Student {
  29. @PrimaryKey
  30. @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  31. private Long id;
  32. private int grade;
  33. @Element(dependent = "true")
  34. private List<Key> courses = new ArrayList<Key>();
  35. @Persistent
  36. private Key major;
  37. public int getGrade() {
  38. return grade;
  39. }
  40. public void setGrade(int grade) {
  41. this.grade = grade;
  42. }
  43. public List<Key> getCourses() {
  44. return courses;
  45. }
  46. public void setCourses(List<Key> courses) {
  47. this.courses = courses;
  48. }
  49. public Key getMajor() {
  50. return major;
  51. }
  52. public void setMajor(Key major) {
  53. this.major = major;
  54. }
  55. @Override
  56. public boolean equals(Object o) {
  57. if (this == o) {
  58. return true;
  59. }
  60. if (o == null || getClass() != o.getClass()) {
  61. return false;
  62. }
  63. Student student = (Student) o;
  64. if (id != null ? !id.equals(student.id) : student.id != null) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. @Override
  70. public int hashCode() {
  71. return id != null ? id.hashCode() : 0;
  72. }
  73. }
  74. @PersistenceCapable(detachable = "true")
  75. public static class Course {
  76. @PrimaryKey
  77. @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  78. private Key key;
  79. private String department;
  80. public Key getKey() {
  81. return key;
  82. }
  83. public String getDepartment() {
  84. return department;
  85. }
  86. public void setDepartment(String department) {
  87. this.department = department;
  88. }
  89. @Override
  90. public boolean equals(Object o) {
  91. if (this == o) {
  92. return true;
  93. }
  94. if (o == null || getClass() != o.getClass()) {
  95. return false;
  96. }
  97. Course course = (Course) o;
  98. if (key != null ? !key.equals(course.key) : course.key != null) {
  99. return false;
  100. }
  101. return true;
  102. }
  103. @Override
  104. public int hashCode() {
  105. return key != null ? key.hashCode() : 0;
  106. }
  107. }
  108. @PersistenceCapable(detachable = "true")
  109. public static class Major {
  110. @PrimaryKey
  111. @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  112. private Key key;
  113. private String school;
  114. public Key getKey() {
  115. return key;
  116. }
  117. public void setKey(Key key) {
  118. this.key = key;
  119. }
  120. public String getSchool() {
  121. return school;
  122. }
  123. public void setSchool(String school) {
  124. this.school = school;
  125. }
  126. @Override
  127. public boolean equals(Object o) {
  128. if (this == o) {
  129. return true;
  130. }
  131. if (o == null || getClass() != o.getClass()) {
  132. return false;
  133. }
  134. Major major = (Major) o;
  135. if (key != null ? !key.equals(major.key) : major.key != null) {
  136. return false;
  137. }
  138. return true;
  139. }
  140. @Override
  141. public int hashCode() {
  142. int result = key != null ? key.hashCode() : 0;
  143. result = 31 * result + (school != null ? school.hashCode() : 0);
  144. return result;
  145. }
  146. }
  147. public static Student newStudent(int grade, Course... courses) {
  148. return newStudent(grade, null, courses);
  149. }
  150. public static Student newStudent(int grade, Major major, Course... courses) {
  151. Student s = new Student();
  152. s.setGrade(grade);
  153. List<Key> courseKeys = Utils.newArrayList();
  154. for (Course c : courses) {
  155. courseKeys.add(c.getKey());
  156. }
  157. s.setCourses(courseKeys);
  158. if (major != null) {
  159. s.setMajor(major.getKey());
  160. }
  161. return s;
  162. }
  163. public static Course newCourse(String dept) {
  164. Course c = new Course();
  165. c.setDepartment(dept);
  166. return c;
  167. }
  168. public static Major newMajor(String school) {
  169. Major m = new Major();
  170. m.setSchool(school);
  171. return m;
  172. }
  173. }