/tests/com/google/appengine/datanucleus/query/JPQLQueryUnownedJoinTest.java

http://datanucleus-appengine.googlecode.com/ · Java · 254 lines · 207 code · 19 blank · 28 comment · 11 complexity · 22d555b20e6375b3eeb662aeac62df14 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.query;
  14. import com.google.appengine.api.datastore.Entity;
  15. import com.google.appengine.api.datastore.EntityNotFoundException;
  16. import com.google.appengine.api.datastore.KeyFactory;
  17. import com.google.appengine.datanucleus.jpa.JPATestCase;
  18. import com.google.appengine.datanucleus.test.jpa.UnownedJoinsJPA.Course;
  19. import com.google.appengine.datanucleus.test.jpa.UnownedJoinsJPA.Major;
  20. import com.google.appengine.datanucleus.test.jpa.UnownedJoinsJPA.Student;
  21. import static com.google.appengine.datanucleus.test.jpa.UnownedJoinsJPA.newCourse;
  22. import static com.google.appengine.datanucleus.test.jpa.UnownedJoinsJPA.newMajor;
  23. import static com.google.appengine.datanucleus.test.jpa.UnownedJoinsJPA.newStudent;
  24. import java.util.Collections;
  25. import javax.persistence.PersistenceException;
  26. import javax.persistence.Query;
  27. /**
  28. * @author Max Ross <maxr@google.com>
  29. */
  30. public class JPQLQueryUnownedJoinTest extends JPATestCase {
  31. public void testTransientMeansTransient() throws EntityNotFoundException {
  32. Student student = new Student();
  33. beginTxn();
  34. em.persist(student);
  35. commitTxn();
  36. Entity studentEntity = ds.get(KeyFactory.createKey(kindForClass(Student.class), student.getId()));
  37. assertEquals(3, studentEntity.getProperties().size());
  38. assertTrue(studentEntity.hasProperty("courses"));
  39. assertTrue(studentEntity.hasProperty("grade"));
  40. assertTrue(studentEntity.hasProperty("major"));
  41. }
  42. public void testJoinOnOneToMany_Simple() {
  43. Course course1 = newCourse("Biology");
  44. Course course2 = newCourse("Not Biology");
  45. persistInTxn(course1, course2);
  46. Student student = newStudent(10, course1, course2);
  47. beginTxn();
  48. em.persist(student);
  49. commitTxn();
  50. beginTxn();
  51. Query q = em.createQuery(
  52. "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
  53. + "c.department = 'Biology' and "
  54. + "s.grade = 10");
  55. assertEquals(student.getId(), ((Student) q.getSingleResult()).getId());
  56. commitTxn();
  57. }
  58. public void testJoinOnOneToMany_LegalOrderBy() {
  59. Course course1 = newCourse("Biology");
  60. Course course2 = newCourse("Not Biology");
  61. persistInTxn(course1, course2);
  62. Student student = newStudent(10, course1, course2);
  63. persistInTxn(student);
  64. beginTxn();
  65. Query q = em.createQuery(
  66. "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
  67. + "c.department = 'Biology' and "
  68. + "s.grade = 10 order by s.courses asc");
  69. assertEquals(student.getId(), ((Student) q.getSingleResult()).getId());
  70. commitTxn();
  71. }
  72. private void persistInTxn(Object... objects) {
  73. for (Object o : objects) {
  74. beginTxn();
  75. em.persist(o);
  76. commitTxn();
  77. }
  78. }
  79. public void testJoinOnOneToMany_Offset() {
  80. Course course1 = newCourse("Biology");
  81. Course course2 = newCourse("Not Biology");
  82. Course course3 = newCourse("Biology");
  83. Course course4 = newCourse("Not Biology");
  84. Course course5 = newCourse("Biology");
  85. Course course6 = newCourse("Not Biology");
  86. persistInTxn(course1, course2, course3, course4, course5, course6);
  87. Student student = newStudent(10, course1, course2);
  88. Student student2 = newStudent(11, course3, course4);
  89. Student student3 = newStudent(10, course5, course6);
  90. persistInTxn(student, student2, student3);
  91. beginTxn();
  92. Query q = em.createQuery(
  93. "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
  94. + "c.department = 'Biology' and "
  95. + "s.grade = 10");
  96. q.setFirstResult(1);
  97. assertEquals(student3.getId(), ((Student) q.getSingleResult()).getId());
  98. q = em.createQuery(
  99. "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
  100. + "c.department = 'Biology' and "
  101. + "s.grade = 10");
  102. q.setFirstResult(2);
  103. assertEquals(Collections.emptyList(), q.getResultList());
  104. commitTxn();
  105. }
  106. public void testJoinOnOneToMany_Limit() {
  107. Course course1 = newCourse("Biology");
  108. Course course2 = newCourse("Not Biology");
  109. Course course3 = newCourse("Biology");
  110. Course course4 = newCourse("Not Biology");
  111. Course course5 = newCourse("Biology");
  112. Course course6 = newCourse("Not Biology");
  113. persistInTxn(course1, course2, course3, course4, course5, course6);
  114. Student student = newStudent(10, course1, course2);
  115. Student student2 = newStudent(11, course3, course4);
  116. Student student3 = newStudent(10, course5, course6);
  117. persistInTxn(student, student2, student3);
  118. beginTxn();
  119. Query q = em.createQuery(
  120. "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
  121. + "c.department = 'Biology' and "
  122. + "s.grade = 10");
  123. q.setMaxResults(1);
  124. assertEquals(student.getId(), ((Student) q.getSingleResult()).getId());
  125. q = em.createQuery(
  126. "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
  127. + "c.department = 'Biology' and "
  128. + "s.grade = 10");
  129. q.setMaxResults(0);
  130. assertEquals(Collections.emptyList(), q.getResultList());
  131. commitTxn();
  132. }
  133. public void testJoinOnOneToOne_Simple() {
  134. Major major1 = newMajor("Liberal Arts");
  135. Major major2 = newMajor("Engineering");
  136. persistInTxn(major1, major2);
  137. Student student1 = newStudent(10, major1);
  138. Student student2 = newStudent(10, major2);
  139. persistInTxn(student1, student2);
  140. beginTxn();
  141. Query q = em.createQuery(
  142. "select from " + Student.class.getName() + " s JOIN s.majorAlias m where "
  143. + "m.school = 'Liberal Arts' and "
  144. + "s.grade = 10");
  145. assertEquals(student1.getId(), ((Student) q.getSingleResult()).getId());
  146. commitTxn();
  147. }
  148. public void testJoinOnOneToMany_Illegal() {
  149. beginTxn();
  150. // all filters on parent must be equality filters
  151. Query q = em.createQuery(
  152. "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
  153. + "c.department = 'Biology' and "
  154. + "s.grade > 10");
  155. try {
  156. q.getResultList();
  157. fail("expected exception");
  158. } catch (PersistenceException pe) {
  159. if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
  160. // good
  161. }
  162. else {
  163. throw pe;
  164. }
  165. }
  166. // all filters on child must be equality filters
  167. q = em.createQuery(
  168. "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
  169. + "c.department > 'Biology' and "
  170. + "s.grade = 10");
  171. try {
  172. q.getResultList();
  173. fail("expected exception");
  174. } catch (PersistenceException pe) {
  175. if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
  176. // good
  177. }
  178. else {
  179. throw pe;
  180. }
  181. }
  182. // sort on parent can only be by join column in asc order
  183. q = em.createQuery(
  184. "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
  185. + "c.department = 'Biology' and "
  186. + "s.grade = 10 order by s.grade");
  187. try {
  188. q.getResultList();
  189. fail("expected exception");
  190. } catch (PersistenceException pe) {
  191. if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
  192. // good
  193. }
  194. else {
  195. throw pe;
  196. }
  197. }
  198. // sort is by the join column but in the wrong order
  199. q = em.createQuery(
  200. "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
  201. + "c.department = 'Biology' and "
  202. + "s.grade = 10 order by s.coursesAlias desc");
  203. try {
  204. q.getResultList();
  205. fail("expected exception");
  206. } catch (PersistenceException pe) {
  207. if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
  208. // good
  209. }
  210. else {
  211. throw pe;
  212. }
  213. }
  214. // can't sort by child property
  215. q = em.createQuery(
  216. "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
  217. + "c.department = 'Biology' and "
  218. + "s.grade = 10 order by c.department");
  219. try {
  220. q.getResultList();
  221. fail("expected exception");
  222. } catch (PersistenceException pe) {
  223. if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
  224. // good
  225. }
  226. else {
  227. throw pe;
  228. }
  229. }
  230. commitTxn();
  231. }
  232. }