PageRenderTime 31ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 347 lines | 240 code | 74 blank | 33 comment | 0 complexity | 63aa8dd4442bd93da30c4dca3f540fbf MD5 | raw file
Possible License(s): Apache-2.0
  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 javax.persistence.CascadeType;
  19. import javax.persistence.Entity;
  20. import javax.persistence.FetchType;
  21. import javax.persistence.GeneratedValue;
  22. import javax.persistence.GenerationType;
  23. import javax.persistence.Id;
  24. import javax.persistence.Inheritance;
  25. import javax.persistence.InheritanceType;
  26. import javax.persistence.OneToOne;
  27. /**
  28. * Example 1:
  29. * A has X
  30. * B extends A has X
  31. * X has a back-pointer to A
  32. *
  33. * Example 2:
  34. * A
  35. * B extends A has Y extends X
  36. * Y has a back-pointer to B
  37. *
  38. * Example 3:
  39. * A has Y extends X
  40. * B extends A
  41. * X has a back-pointer to A
  42. *
  43. * @author Max Ross <max.ross@gmail.com>
  44. */
  45. public class BidirectionalOneToOneSubclassesJPA {
  46. public static class Example1 {
  47. @Entity
  48. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  49. public static class A {
  50. @Id
  51. @GeneratedValue(strategy= GenerationType.IDENTITY)
  52. private Long id;
  53. private String aString;
  54. @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  55. private X child;
  56. public Long getId() {
  57. return id;
  58. }
  59. public void setId(Long id) {
  60. this.id = id;
  61. }
  62. public String getAString() {
  63. return aString;
  64. }
  65. public void setAString(String aString) {
  66. this.aString = aString;
  67. }
  68. public X getChild() {
  69. return child;
  70. }
  71. public void setChild(X child) {
  72. this.child = child;
  73. }
  74. }
  75. @Entity
  76. public static class B extends A {
  77. private String bString;
  78. public String getBString() {
  79. return bString;
  80. }
  81. public void setBString(String bString) {
  82. this.bString = bString;
  83. }
  84. }
  85. @Entity
  86. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  87. public static class X {
  88. @Id
  89. @GeneratedValue(strategy= GenerationType.IDENTITY)
  90. private Key id;
  91. @OneToOne(fetch = FetchType.LAZY, mappedBy = "child")
  92. private A parent;
  93. private String xString;
  94. public Key getId() {
  95. return id;
  96. }
  97. public void setId(Key id) {
  98. this.id = id;
  99. }
  100. public void setParent(A parent) {
  101. this.parent = parent;
  102. }
  103. public A getParent() {
  104. return parent;
  105. }
  106. public String getXString() {
  107. return xString;
  108. }
  109. public void setXString(String xString) {
  110. this.xString = xString;
  111. }
  112. }
  113. @Entity
  114. public static class Y extends X {
  115. }
  116. }
  117. public static class Example2 {
  118. @Entity
  119. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  120. public static class A {
  121. @Id
  122. @GeneratedValue(strategy= GenerationType.IDENTITY)
  123. private Long id;
  124. private String aString;
  125. public Long getId() {
  126. return id;
  127. }
  128. public void setId(Long id) {
  129. this.id = id;
  130. }
  131. public String getAString() {
  132. return aString;
  133. }
  134. public void setAString(String aString) {
  135. this.aString = aString;
  136. }
  137. }
  138. @Entity
  139. public static class B extends A {
  140. @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  141. private Y child;
  142. private String bString;
  143. public String getBString() {
  144. return bString;
  145. }
  146. public void setBString(String bString) {
  147. this.bString = bString;
  148. }
  149. public Y getChild() {
  150. return child;
  151. }
  152. public void setChild(Y child) {
  153. this.child = child;
  154. }
  155. }
  156. @Entity
  157. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  158. public static class X {
  159. @Id
  160. @GeneratedValue(strategy= GenerationType.IDENTITY)
  161. private Key id;
  162. private String xString;
  163. public Key getId() {
  164. return id;
  165. }
  166. public void setId(Key id) {
  167. this.id = id;
  168. }
  169. public String getXString() {
  170. return xString;
  171. }
  172. public void setXString(String xString) {
  173. this.xString = xString;
  174. }
  175. }
  176. @Entity
  177. public static class Y extends X{
  178. private String yString;
  179. @OneToOne(fetch = FetchType.LAZY, mappedBy = "child")
  180. private B parent;
  181. public String getYString() {
  182. return yString;
  183. }
  184. public void setYString(String yString) {
  185. this.yString = yString;
  186. }
  187. public B getParent() {
  188. return parent;
  189. }
  190. public void setParent(B parent) {
  191. this.parent = parent;
  192. }
  193. }
  194. }
  195. public static class Example3 {
  196. @Entity
  197. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  198. public static class A {
  199. @Id
  200. @GeneratedValue(strategy= GenerationType.IDENTITY)
  201. private Long id;
  202. private String aString;
  203. @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  204. private Y child;
  205. public Long getId() {
  206. return id;
  207. }
  208. public void setId(Long id) {
  209. this.id = id;
  210. }
  211. public String getAString() {
  212. return aString;
  213. }
  214. public void setAString(String aString) {
  215. this.aString = aString;
  216. }
  217. public Y getChild() {
  218. return child;
  219. }
  220. public void setChild(Y child) {
  221. this.child = child;
  222. }
  223. }
  224. @Entity
  225. public static class B extends A {
  226. private String bString;
  227. public String getBString() {
  228. return bString;
  229. }
  230. public void setBString(String bString) {
  231. this.bString = bString;
  232. }
  233. }
  234. @Entity
  235. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  236. public static class X {
  237. @Id
  238. @GeneratedValue(strategy= GenerationType.IDENTITY)
  239. private Key id;
  240. @OneToOne(fetch = FetchType.LAZY, mappedBy = "child")
  241. private A parent;
  242. private String xString;
  243. public Key getId() {
  244. return id;
  245. }
  246. public void setId(Key id) {
  247. this.id = id;
  248. }
  249. public String getXString() {
  250. return xString;
  251. }
  252. public void setXString(String xString) {
  253. this.xString = xString;
  254. }
  255. public void setParent(A a) {
  256. this.parent = a;
  257. }
  258. public A getParent() {
  259. return parent;
  260. }
  261. }
  262. @Entity
  263. public static class Y extends X {
  264. private String yString;
  265. public String getYString() {
  266. return yString;
  267. }
  268. public void setYString(String yString) {
  269. this.yString = yString;
  270. }
  271. }
  272. }
  273. }