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

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