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

http://datanucleus-appengine.googlecode.com/ · Java · 130 lines · 90 code · 25 blank · 15 comment · 11 complexity · 3dbdacfc0f13233b3de5c732df186957 MD5 · raw file

  1. /**********************************************************************
  2. Copyright (c) 2011 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.test;
  14. import com.google.appengine.api.datastore.KeyFactory;
  15. import org.datanucleus.api.jpa.annotations.Extension;
  16. import javax.persistence.DiscriminatorValue;
  17. import javax.persistence.Entity;
  18. import javax.persistence.GeneratedValue;
  19. import javax.persistence.GenerationType;
  20. import javax.persistence.Id;
  21. public class UnidirectionalSingeTableChildJPA {
  22. public static final String DISCRIMINATOR_TOP = "Top";
  23. public static final String DISCRIMINATOR_MIDDLE = "Middle";
  24. public static final String DISCRIMINATOR_BOTTOM = "Bottom";
  25. @Entity
  26. @DiscriminatorValue(value = DISCRIMINATOR_TOP)
  27. public static class UnidirTop {
  28. @Id
  29. @GeneratedValue(strategy=GenerationType.IDENTITY)
  30. @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
  31. private String id;
  32. private String str;
  33. private String name;
  34. public UnidirTop(String namedKey) {
  35. this.id = namedKey == null ? null :
  36. KeyFactory.keyToString(KeyFactory.createKey(UnidirectionalSingeTableChildJPA.getEntityKind(UnidirTop.class), namedKey));
  37. }
  38. public void setId(String id) {
  39. this.id = id;
  40. }
  41. public UnidirTop() {
  42. this(null);
  43. }
  44. public String getId() {
  45. return id;
  46. }
  47. public String getStr() {
  48. return str;
  49. }
  50. public void setStr(String str) {
  51. this.str = str;
  52. }
  53. public String getName() {
  54. return name;
  55. }
  56. public void setName(String name) {
  57. this.name = name;
  58. }
  59. @Override
  60. public String toString() {
  61. return "\n\nid: " + id + "\nstr: " + str + "\nname: " + name;
  62. }
  63. @Override
  64. public boolean equals(Object o) {
  65. if (this == o) {
  66. return true;
  67. }
  68. if (o == null || getClass() != o.getClass()) {
  69. return false;
  70. }
  71. UnidirTop book = (UnidirTop) o;
  72. if (id != null ? !id.equals(book.id) : book.id != null) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. @Override
  78. public int hashCode() {
  79. return (id != null ? id.hashCode() : 0);
  80. }
  81. }
  82. @Entity
  83. @DiscriminatorValue(value = DISCRIMINATOR_MIDDLE)
  84. public static class UnidirMiddle extends UnidirTop {
  85. public UnidirMiddle() {
  86. this(null);
  87. }
  88. public UnidirMiddle(String namedKey) {
  89. super(namedKey);
  90. }
  91. }
  92. @Entity
  93. @DiscriminatorValue(value = DISCRIMINATOR_BOTTOM)
  94. public static class UnidirBottom extends UnidirMiddle {
  95. public UnidirBottom(String namedKey) {
  96. super(namedKey);
  97. }
  98. }
  99. public static String getEntityKind(Class<?> clazz) {
  100. return clazz.getName().substring(clazz.getPackage().getName().length() + 1);
  101. }
  102. }