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

http://datanucleus-appengine.googlecode.com/ · Java · 453 lines · 315 code · 94 blank · 44 comment · 0 complexity · 4868b6ab496c26ac40665cd4bb492ee8 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 Google Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.appengine.datanucleus.test;
  17. import com.google.appengine.api.datastore.Key;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import javax.persistence.CascadeType;
  21. import javax.persistence.Entity;
  22. import javax.persistence.FetchType;
  23. import javax.persistence.GeneratedValue;
  24. import javax.persistence.GenerationType;
  25. import javax.persistence.Id;
  26. import javax.persistence.Inheritance;
  27. import javax.persistence.InheritanceType;
  28. import javax.persistence.ManyToOne;
  29. import javax.persistence.OneToMany;
  30. import javax.persistence.OrderBy;
  31. /**
  32. * Example 1 (A-X):
  33. * A has List<X>
  34. * B extends A
  35. * X has back-pointer to A
  36. * Y extends X
  37. *
  38. * Example 2 (B-Y):
  39. * A
  40. * B extends A has List<Y extends X>
  41. * X
  42. * Y extends X, has a back-pointer to A
  43. *
  44. * Example 3 (B-X):
  45. * A
  46. * B extends A has List<X>
  47. * X has back-pointer to B
  48. * Y extends X
  49. *
  50. * Example 4 (A-Y):
  51. * A has List<Y extends X>
  52. * B extends A
  53. * X has a back-pointer to A
  54. * Y extends X
  55. *
  56. * @author Max Ross <max.ross@gmail.com>
  57. */
  58. public class BidirectionalOneToManySubclassesJPA {
  59. public static class Example1 {
  60. @Entity
  61. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  62. public static class A {
  63. @Id
  64. @GeneratedValue(strategy= GenerationType.IDENTITY)
  65. private Long id;
  66. private String aString;
  67. @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  68. @OrderBy(value = "xString desc")
  69. private List<X> children = new ArrayList<X>();
  70. public Long getId() {
  71. return id;
  72. }
  73. public void setId(Long id) {
  74. this.id = id;
  75. }
  76. public String getAString() {
  77. return aString;
  78. }
  79. public void setAString(String aString) {
  80. this.aString = aString;
  81. }
  82. public List<X> getChildren() {
  83. return children;
  84. }
  85. public void setChildren(List<X> children) {
  86. this.children = children;
  87. }
  88. }
  89. @Entity
  90. public static class B extends A {
  91. private String bString;
  92. public String getBString() {
  93. return bString;
  94. }
  95. public void setBString(String bString) {
  96. this.bString = bString;
  97. }
  98. }
  99. @Entity
  100. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  101. public static class X {
  102. @Id
  103. @GeneratedValue(strategy= GenerationType.IDENTITY)
  104. private Key id;
  105. @SuppressWarnings("unused")
  106. @ManyToOne(fetch = FetchType.LAZY)
  107. private A parent;
  108. private String xString;
  109. public Key getId() {
  110. return id;
  111. }
  112. public void setId(Key id) {
  113. this.id = id;
  114. }
  115. public String getXString() {
  116. return xString;
  117. }
  118. public void setXString(String xString) {
  119. this.xString = xString;
  120. }
  121. }
  122. @Entity
  123. public static class Y extends X {
  124. }
  125. }
  126. public static class Example2 {
  127. @Entity
  128. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  129. public static class A {
  130. @Id
  131. @GeneratedValue(strategy= GenerationType.IDENTITY)
  132. private Long id;
  133. private String aString;
  134. public Long getId() {
  135. return id;
  136. }
  137. public void setId(Long id) {
  138. this.id = id;
  139. }
  140. public String getAString() {
  141. return aString;
  142. }
  143. public void setAString(String aString) {
  144. this.aString = aString;
  145. }
  146. }
  147. @Entity
  148. public static class B extends A {
  149. @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  150. @OrderBy(value = "xString desc")
  151. private List<Y> children = new ArrayList<Y>();
  152. private String bString;
  153. public String getBString() {
  154. return bString;
  155. }
  156. public void setBString(String bString) {
  157. this.bString = bString;
  158. }
  159. public List<Y> getChildren() {
  160. return children;
  161. }
  162. public void setChildren(List<Y> children) {
  163. this.children = children;
  164. }
  165. }
  166. @Entity
  167. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  168. public static class X {
  169. @Id
  170. @GeneratedValue(strategy= GenerationType.IDENTITY)
  171. private Key id;
  172. private String xString;
  173. public Key getId() {
  174. return id;
  175. }
  176. public void setId(Key id) {
  177. this.id = id;
  178. }
  179. public String getXString() {
  180. return xString;
  181. }
  182. public void setXString(String xString) {
  183. this.xString = xString;
  184. }
  185. }
  186. @Entity
  187. public static class Y extends X {
  188. @ManyToOne(fetch = FetchType.LAZY)
  189. private B parent;
  190. private String yString;
  191. public B getParent() {
  192. return parent;
  193. }
  194. public String getYString() {
  195. return yString;
  196. }
  197. public void setYString(String yString) {
  198. this.yString = yString;
  199. }
  200. }
  201. }
  202. public static class Example3 {
  203. @Entity
  204. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  205. public static class A {
  206. @Id
  207. @GeneratedValue(strategy= GenerationType.IDENTITY)
  208. private Long id;
  209. private String aString;
  210. public Long getId() {
  211. return id;
  212. }
  213. public void setId(Long id) {
  214. this.id = id;
  215. }
  216. public String getAString() {
  217. return aString;
  218. }
  219. public void setAString(String aString) {
  220. this.aString = aString;
  221. }
  222. }
  223. @Entity
  224. public static class B extends A {
  225. // TODO This ought to be generic of X not Y!
  226. @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  227. @OrderBy(value = "xString desc")
  228. private List<Y> children = new ArrayList<Y>();
  229. private String bString;
  230. public String getBString() {
  231. return bString;
  232. }
  233. public void setBString(String bString) {
  234. this.bString = bString;
  235. }
  236. public List<Y> getChildren() {
  237. return children;
  238. }
  239. public void setChildren(List<Y> children) {
  240. this.children = children;
  241. }
  242. }
  243. @Entity
  244. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  245. public static class X {
  246. @Id
  247. @GeneratedValue(strategy= GenerationType.IDENTITY)
  248. private Key id;
  249. private String xString;
  250. public Key getId() {
  251. return id;
  252. }
  253. public void setId(Key id) {
  254. this.id = id;
  255. }
  256. public String getXString() {
  257. return xString;
  258. }
  259. public void setXString(String xString) {
  260. this.xString = xString;
  261. }
  262. }
  263. @Entity
  264. public static class Y extends X{
  265. // TODO This ought to be on X not Y!
  266. @ManyToOne(fetch = FetchType.LAZY)
  267. private B parent;
  268. public B getParent() {
  269. return parent;
  270. }
  271. public void setParent(B parent) {
  272. this.parent = parent;
  273. }
  274. private String yString;
  275. public String getYString() {
  276. return yString;
  277. }
  278. public void setYString(String yString) {
  279. this.yString = yString;
  280. }
  281. }
  282. }
  283. public static class Example4 {
  284. @Entity
  285. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  286. public static class A {
  287. @Id
  288. @GeneratedValue(strategy= GenerationType.IDENTITY)
  289. private Long id;
  290. private String aString;
  291. @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  292. @OrderBy(value = "xString desc")
  293. private List<Y> children = new ArrayList<Y>();
  294. public Long getId() {
  295. return id;
  296. }
  297. public void setId(Long id) {
  298. this.id = id;
  299. }
  300. public String getAString() {
  301. return aString;
  302. }
  303. public void setAString(String aString) {
  304. this.aString = aString;
  305. }
  306. public List<Y> getChildren() {
  307. return children;
  308. }
  309. public void setChildren(List<Y> children) {
  310. this.children = children;
  311. }
  312. }
  313. @Entity
  314. public static class B extends A {
  315. private String bString;
  316. public String getBString() {
  317. return bString;
  318. }
  319. public void setBString(String bString) {
  320. this.bString = bString;
  321. }
  322. }
  323. @Entity
  324. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  325. public static class X {
  326. @Id
  327. @GeneratedValue(strategy= GenerationType.IDENTITY)
  328. private Key id;
  329. private String xString;
  330. public Key getId() {
  331. return id;
  332. }
  333. public void setId(Key id) {
  334. this.id = id;
  335. }
  336. public String getXString() {
  337. return xString;
  338. }
  339. public void setXString(String xString) {
  340. this.xString = xString;
  341. }
  342. }
  343. @Entity
  344. public static class Y extends X {
  345. @ManyToOne(fetch = FetchType.LAZY)
  346. private A parent;
  347. private String yString;
  348. public A getParent() {
  349. return parent;
  350. }
  351. public String getYString() {
  352. return yString;
  353. }
  354. public void setYString(String yString) {
  355. this.yString = yString;
  356. }
  357. }
  358. }
  359. }