/tests/com/google/appengine/datanucleus/test/BidirectionalOneToManySubclassesJPA.java
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 */ 16package com.google.appengine.datanucleus.test; 17 18import com.google.appengine.api.datastore.Key; 19 20import java.util.ArrayList; 21import java.util.List; 22 23import javax.persistence.CascadeType; 24import javax.persistence.Entity; 25import javax.persistence.FetchType; 26import javax.persistence.GeneratedValue; 27import javax.persistence.GenerationType; 28import javax.persistence.Id; 29import javax.persistence.Inheritance; 30import javax.persistence.InheritanceType; 31import javax.persistence.ManyToOne; 32import javax.persistence.OneToMany; 33import javax.persistence.OrderBy; 34 35/** 36 * Example 1 (A-X): 37 * A has List<X> 38 * B extends A 39 * X has back-pointer to A 40 * Y extends X 41 * 42 * Example 2 (B-Y): 43 * A 44 * B extends A has List<Y extends X> 45 * X 46 * Y extends X, has a back-pointer to A 47 * 48 * Example 3 (B-X): 49 * A 50 * B extends A has List<X> 51 * X has back-pointer to B 52 * Y extends X 53 * 54 * Example 4 (A-Y): 55 * A has List<Y extends X> 56 * B extends A 57 * X has a back-pointer to A 58 * Y extends X 59 * 60 * @author Max Ross <max.ross@gmail.com> 61 */ 62public class BidirectionalOneToManySubclassesJPA { 63 64 public static class Example1 { 65 @Entity 66 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 67 public static class A { 68 @Id 69 @GeneratedValue(strategy= GenerationType.IDENTITY) 70 private Long id; 71 72 private String aString; 73 74 @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 75 @OrderBy(value = "xString desc") 76 private List<X> children = new ArrayList<X>(); 77 78 public Long getId() { 79 return id; 80 } 81 82 public void setId(Long id) { 83 this.id = id; 84 } 85 86 public String getAString() { 87 return aString; 88 } 89 90 public void setAString(String aString) { 91 this.aString = aString; 92 } 93 94 public List<X> getChildren() { 95 return children; 96 } 97 98 public void setChildren(List<X> children) { 99 this.children = children; 100 } 101 } 102 103 @Entity 104 public static class B extends A { 105 private String bString; 106 107 public String getBString() { 108 return bString; 109 } 110 111 public void setBString(String bString) { 112 this.bString = bString; 113 } 114 } 115 116 @Entity 117 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 118 public static class X { 119 @Id 120 @GeneratedValue(strategy= GenerationType.IDENTITY) 121 private Key id; 122 123 @SuppressWarnings("unused") 124 @ManyToOne(fetch = FetchType.LAZY) 125 private A parent; 126 127 private String xString; 128 129 public Key getId() { 130 return id; 131 } 132 133 public void setId(Key id) { 134 this.id = id; 135 } 136 137 public String getXString() { 138 return xString; 139 } 140 141 public void setXString(String xString) { 142 this.xString = xString; 143 } 144 } 145 146 @Entity 147 public static class Y extends X { 148 } 149 } 150 151 public static class Example2 { 152 @Entity 153 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 154 public static class A { 155 @Id 156 @GeneratedValue(strategy= GenerationType.IDENTITY) 157 private Long id; 158 159 private String aString; 160 161 public Long getId() { 162 return id; 163 } 164 165 public void setId(Long id) { 166 this.id = id; 167 } 168 169 public String getAString() { 170 return aString; 171 } 172 173 public void setAString(String aString) { 174 this.aString = aString; 175 } 176 } 177 178 @Entity 179 public static class B extends A { 180 @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 181 @OrderBy(value = "xString desc") 182 private List<Y> children = new ArrayList<Y>(); 183 184 private String bString; 185 186 public String getBString() { 187 return bString; 188 } 189 190 public void setBString(String bString) { 191 this.bString = bString; 192 } 193 194 public List<Y> getChildren() { 195 return children; 196 } 197 198 public void setChildren(List<Y> children) { 199 this.children = children; 200 } 201 } 202 203 @Entity 204 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 205 public static class X { 206 @Id 207 @GeneratedValue(strategy= GenerationType.IDENTITY) 208 private Key id; 209 210 private String xString; 211 212 public Key getId() { 213 return id; 214 } 215 216 public void setId(Key id) { 217 this.id = id; 218 } 219 220 public String getXString() { 221 return xString; 222 } 223 224 public void setXString(String xString) { 225 this.xString = xString; 226 } 227 } 228 229 @Entity 230 public static class Y extends X { 231 @ManyToOne(fetch = FetchType.LAZY) 232 private B parent; 233 234 private String yString; 235 236 public B getParent() { 237 return parent; 238 } 239 240 public String getYString() { 241 return yString; 242 } 243 244 public void setYString(String yString) { 245 this.yString = yString; 246 } 247 } 248 } 249 250 public static class Example3 { 251 @Entity 252 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 253 public static class A { 254 @Id 255 @GeneratedValue(strategy= GenerationType.IDENTITY) 256 private Long id; 257 258 private String aString; 259 260 public Long getId() { 261 return id; 262 } 263 264 public void setId(Long id) { 265 this.id = id; 266 } 267 268 public String getAString() { 269 return aString; 270 } 271 272 public void setAString(String aString) { 273 this.aString = aString; 274 } 275 } 276 277 @Entity 278 public static class B extends A { 279 // TODO This ought to be generic of X not Y! 280 @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 281 @OrderBy(value = "xString desc") 282 private List<Y> children = new ArrayList<Y>(); 283 284 private String bString; 285 286 public String getBString() { 287 return bString; 288 } 289 290 public void setBString(String bString) { 291 this.bString = bString; 292 } 293 294 public List<Y> getChildren() { 295 return children; 296 } 297 298 public void setChildren(List<Y> children) { 299 this.children = children; 300 } 301 } 302 303 @Entity 304 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 305 public static class X { 306 @Id 307 @GeneratedValue(strategy= GenerationType.IDENTITY) 308 private Key id; 309 310 private String xString; 311 312 public Key getId() { 313 return id; 314 } 315 316 public void setId(Key id) { 317 this.id = id; 318 } 319 320 public String getXString() { 321 return xString; 322 } 323 324 public void setXString(String xString) { 325 this.xString = xString; 326 } 327 } 328 329 @Entity 330 public static class Y extends X{ 331 // TODO This ought to be on X not Y! 332 @ManyToOne(fetch = FetchType.LAZY) 333 private B parent; 334 335 public B getParent() { 336 return parent; 337 } 338 339 public void setParent(B parent) { 340 this.parent = parent; 341 } 342 343 private String yString; 344 345 public String getYString() { 346 return yString; 347 } 348 349 public void setYString(String yString) { 350 this.yString = yString; 351 } 352 } 353 } 354 355 public static class Example4 { 356 @Entity 357 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 358 public static class A { 359 @Id 360 @GeneratedValue(strategy= GenerationType.IDENTITY) 361 private Long id; 362 363 private String aString; 364 365 @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 366 @OrderBy(value = "xString desc") 367 private List<Y> children = new ArrayList<Y>(); 368 369 public Long getId() { 370 return id; 371 } 372 373 public void setId(Long id) { 374 this.id = id; 375 } 376 377 public String getAString() { 378 return aString; 379 } 380 381 public void setAString(String aString) { 382 this.aString = aString; 383 } 384 385 public List<Y> getChildren() { 386 return children; 387 } 388 389 public void setChildren(List<Y> children) { 390 this.children = children; 391 } 392 } 393 394 @Entity 395 public static class B extends A { 396 private String bString; 397 398 public String getBString() { 399 return bString; 400 } 401 402 public void setBString(String bString) { 403 this.bString = bString; 404 } 405 } 406 407 @Entity 408 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 409 public static class X { 410 @Id 411 @GeneratedValue(strategy= GenerationType.IDENTITY) 412 private Key id; 413 414 private String xString; 415 416 public Key getId() { 417 return id; 418 } 419 420 public void setId(Key id) { 421 this.id = id; 422 } 423 424 public String getXString() { 425 return xString; 426 } 427 428 public void setXString(String xString) { 429 this.xString = xString; 430 } 431 } 432 433 @Entity 434 public static class Y extends X { 435 @ManyToOne(fetch = FetchType.LAZY) 436 private A parent; 437 438 private String yString; 439 440 public A getParent() { 441 return parent; 442 } 443 444 public String getYString() { 445 return yString; 446 } 447 448 public void setYString(String yString) { 449 this.yString = yString; 450 } 451 } 452 } 453}