code_junkie

Why @OneToMany does not work with inheritance in Hibernate

Nov 10th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. @Entity
  2. @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
  3. public class Problem {
  4. @ManyToOne
  5. private Person person;
  6. }
  7.  
  8. @Entity
  9. @DiscriminatorValue("UP")
  10. public class UglyProblem extends Problem {}
  11.  
  12. @Entity
  13. public class Person {
  14. @OneToMany(mappedBy="person")
  15. private List< UglyProblem > problems;
  16. }
  17.  
  18. _____________
  19. |__PROBLEMS__| |__PEOPLE__|
  20. |id <PK> | | |
  21. |person <FK> | -------->| |
  22. |problemType | |_________ |
  23. --------------
  24.  
  25. public interface Problem {
  26. public Person getPerson();
  27. }
  28.  
  29. public interface UglyProblem extends Problem {
  30. }
  31.  
  32. @MappedSuperclass
  33. public abstract class AbstractProblemImpl implements Problem {
  34. @ManyToOne
  35. private Person person;
  36.  
  37. public Person getPerson() {
  38. return person;
  39. }
  40. }
  41.  
  42. @Entity
  43. public class ProblemImpl extends AbstractProblemImpl implements Problem {
  44. }
  45.  
  46. @Entity
  47. public class UglyProblemImpl extends AbstractProblemImpl implements UglyProblem {
  48. }
  49.  
  50. public class A {
  51. private int foo;
  52. }
  53.  
  54. @Entity
  55. public class B extens A {
  56. }
  57.  
  58. @Entity
  59. public class Problem {
  60.  
  61. @Id
  62. @GeneratedValue(strategy = GenerationType.AUTO)
  63. private Long id;
  64.  
  65. private String name;
  66.  
  67. public Long getId() {
  68. return id;
  69. }
  70.  
  71. public void setId(Long id) {
  72. this.id = id;
  73. }
  74.  
  75. public String getName() {
  76. return name;
  77. }
  78.  
  79. public void setName(String name) {
  80. this.name = name;
  81. }
  82. }
  83.  
  84. @Entity
  85. public class UglyProblem extends Problem {
  86.  
  87. private int levelOfUgliness;
  88.  
  89. public int getLevelOfUgliness() {
  90. return levelOfUgliness;
  91. }
  92.  
  93. public void setLevelOfUgliness(int levelOfUgliness) {
  94. this.levelOfUgliness = levelOfUgliness;
  95. }
  96. }
  97.  
  98. CREATE TABLE "problem" (
  99. "DTYPE" varchar(31) NOT NULL,
  100. "id" bigint(20) NOT NULL auto_increment,
  101. "name" varchar(255) default NULL,
  102. "levelOfUgliness" int(11) default NULL,
  103. PRIMARY KEY ("id")
  104. ) AUTO_INCREMENT=2;
  105.  
  106. @Entity
  107. @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
  108. @ForceDiscriminator
  109. public class Problem {
  110.  
  111. }
  112.  
  113. @Entity
  114. @DiscriminatorValue("UP")
  115. public class UglyProblem extends Problem {
  116. @ManyToOne
  117. private Person person;
  118. }
  119.  
  120. @Entity
  121. public class Person {
  122. @OneToMany(mappedBy="person")
  123. private List< UglyProblem > problems;
  124. }
Add Comment
Please, Sign In to add comment