PageRenderTime 53ms CodeModel.GetById 11ms app.highlight 38ms RepoModel.GetById 1ms 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
  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 javax.persistence.CascadeType;
 21import javax.persistence.Entity;
 22import javax.persistence.FetchType;
 23import javax.persistence.GeneratedValue;
 24import javax.persistence.GenerationType;
 25import javax.persistence.Id;
 26import javax.persistence.Inheritance;
 27import javax.persistence.InheritanceType;
 28import javax.persistence.OneToOne;
 29
 30/**
 31 * Example 1:
 32 * A has X
 33 * B extends A has X
 34 * X has a back-pointer to A
 35 *
 36 * Example 2:
 37 * A
 38 * B extends A has Y extends X
 39 * Y has a back-pointer to B
 40 *
 41 * Example 3:
 42 * A has Y extends X
 43 * B extends A
 44 * X has a back-pointer to A
 45 *
 46 * @author Max Ross <max.ross@gmail.com>
 47 */
 48public class BidirectionalOneToOneSubclassesJPA {
 49
 50  public static class Example1 {
 51    @Entity
 52    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 53    public static class A {
 54      @Id
 55      @GeneratedValue(strategy= GenerationType.IDENTITY)
 56      private Long id;
 57
 58      private String aString;
 59
 60      @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
 61      private X child;
 62
 63      public Long getId() {
 64        return id;
 65      }
 66
 67      public void setId(Long id) {
 68        this.id = id;
 69      }
 70
 71      public String getAString() {
 72        return aString;
 73      }
 74
 75      public void setAString(String aString) {
 76        this.aString = aString;
 77      }
 78
 79      public X getChild() {
 80        return child;
 81      }
 82
 83      public void setChild(X child) {
 84        this.child = child;
 85      }
 86    }
 87
 88    @Entity
 89    public static class B extends A {
 90      private String bString;
 91
 92      public String getBString() {
 93        return bString;
 94      }
 95
 96      public void setBString(String bString) {
 97        this.bString = bString;
 98      }
 99    }
100
101    @Entity
102    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
103    public static class X {
104      @Id
105      @GeneratedValue(strategy= GenerationType.IDENTITY)
106      private Key id;
107
108      @OneToOne(fetch = FetchType.LAZY, mappedBy = "child")
109      private A parent;
110
111      private String xString;
112
113      public Key getId() {
114        return id;
115      }
116
117      public void setId(Key id) {
118        this.id = id;
119      }
120
121      public void setParent(A parent) {
122        this.parent = parent;
123      }
124
125      public A getParent() {
126        return parent;
127      }
128
129      public String getXString() {
130        return xString;
131      }
132
133      public void setXString(String xString) {
134        this.xString = xString;
135      }
136    }
137
138    @Entity
139    public static class Y extends X {
140    }
141  }
142
143  public static class Example2 {
144    @Entity
145    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
146    public static class A {
147      @Id
148      @GeneratedValue(strategy= GenerationType.IDENTITY)
149      private Long id;
150
151      private String aString;
152
153      public Long getId() {
154        return id;
155      }
156
157      public void setId(Long id) {
158        this.id = id;
159      }
160
161      public String getAString() {
162        return aString;
163      }
164
165      public void setAString(String aString) {
166        this.aString = aString;
167      }
168    }
169
170    @Entity
171    public static class B extends A {
172
173      @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
174      private Y child;
175
176      private String bString;
177
178      public String getBString() {
179        return bString;
180      }
181
182      public void setBString(String bString) {
183        this.bString = bString;
184      }
185
186      public Y getChild() {
187        return child;
188      }
189
190      public void setChild(Y child) {
191        this.child = child;
192      }
193    }
194
195    @Entity
196    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
197    public static class X {
198      @Id
199      @GeneratedValue(strategy= GenerationType.IDENTITY)
200      private Key id;
201
202      private String xString;
203
204      public Key getId() {
205        return id;
206      }
207
208      public void setId(Key id) {
209        this.id = id;
210      }
211
212      public String getXString() {
213        return xString;
214      }
215
216      public void setXString(String xString) {
217        this.xString = xString;
218      }
219    }
220
221    @Entity
222    public static class Y extends X{
223      private String yString;
224
225      @OneToOne(fetch = FetchType.LAZY, mappedBy = "child")
226      private B parent;
227
228      public String getYString() {
229        return yString;
230      }
231
232      public void setYString(String yString) {
233        this.yString = yString;
234      }
235
236      public B getParent() {
237        return parent;
238      }
239
240      public void setParent(B parent) {
241        this.parent = parent;
242      }
243    }
244  }
245
246  public static class Example3 {
247    @Entity
248    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
249    public static class A {
250      @Id
251      @GeneratedValue(strategy= GenerationType.IDENTITY)
252      private Long id;
253
254      private String aString;
255
256      @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
257      private Y child;
258
259      public Long getId() {
260        return id;
261      }
262
263      public void setId(Long id) {
264        this.id = id;
265      }
266
267      public String getAString() {
268        return aString;
269      }
270
271      public void setAString(String aString) {
272        this.aString = aString;
273      }
274
275      public Y getChild() {
276        return child;
277      }
278
279      public void setChild(Y child) {
280        this.child = child;
281      }
282    }
283
284    @Entity
285    public static class B extends A {
286      private String bString;
287
288      public String getBString() {
289        return bString;
290      }
291
292      public void setBString(String bString) {
293        this.bString = bString;
294      }
295    }
296
297    @Entity
298    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
299    public static class X {
300      @Id
301      @GeneratedValue(strategy= GenerationType.IDENTITY)
302      private Key id;
303
304      @OneToOne(fetch = FetchType.LAZY, mappedBy = "child")
305      private A parent;
306
307      private String xString;
308
309      public Key getId() {
310        return id;
311      }
312
313      public void setId(Key id) {
314        this.id = id;
315      }
316
317      public String getXString() {
318        return xString;
319      }
320
321      public void setXString(String xString) {
322        this.xString = xString;
323      }
324
325      public void setParent(A a) {
326        this.parent = a;
327      }
328
329      public A getParent() {
330        return parent;
331      }
332    }
333
334    @Entity
335    public static class Y extends X {
336      private String yString;
337
338      public String getYString() {
339        return yString;
340      }
341
342      public void setYString(String yString) {
343        this.yString = yString;
344      }
345    }
346  }
347}