Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy @OneToMany does not work with inheritance in Hibernate
    primarykey
    data
    text
    <pre><code>@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Problem { @ManyToOne private Person person; } @Entity @DiscriminatorValue("UP") public class UglyProblem extends Problem {} @Entity public class Person { @OneToMany(mappedBy="person") private List&lt; UglyProblem &gt; problems; } </code></pre> <p>I think it is pretty clear what I am trying to do. I expect @ManyToOne person to be inherited by UglyProblem class. But there will be an exception saying something like: "There is no such property found in UglyProblem class (mappedBy="person")".</p> <p>All I found is <a href="http://opensource.atlassian.com/projects/hibernate/browse/ANN-558" rel="nofollow noreferrer">this</a>. I was not able to find the post by Emmanuel Bernard explaining reasons behind this. </p> <hr> <blockquote> <p>Unfortunately, according to the Hibernate documentation "Properties from superclasses not mapped as @MappedSuperclass are ignored."</p> </blockquote> <p>Well I think this means that if I have these two classes:</p> <pre><code>public class A { private int foo; } @Entity public class B extens A { } </code></pre> <p>then field <code>foo</code> will not be mapped for class B. Which makes sense. But if I have something like this:</p> <pre><code>@Entity public class Problem { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } @Entity public class UglyProblem extends Problem { private int levelOfUgliness; public int getLevelOfUgliness() { return levelOfUgliness; } public void setLevelOfUgliness(int levelOfUgliness) { this.levelOfUgliness = levelOfUgliness; } } </code></pre> <p>I expect the class UglyProblem to have fileds <code>id</code> and <code>name</code> and both classes to be mapped using same table. (In fact, this is exactly what happens, I have just checked again). I have got this table:</p> <pre><code>CREATE TABLE "problem" ( "DTYPE" varchar(31) NOT NULL, "id" bigint(20) NOT NULL auto_increment, "name" varchar(255) default NULL, "levelOfUgliness" int(11) default NULL, PRIMARY KEY ("id") ) AUTO_INCREMENT=2; </code></pre> <p>Going back to my question:</p> <blockquote> <p>I expect @ManyToOne person to be inherited by UglyProblem class.</p> </blockquote> <p>I expect that because all other mapped fields are inherited and I do not see any reason to make this exception for ManyToOne relationships.</p> <hr> <p>Yeah, I saw that. In fact, I used Read-Only solution for my case. But my question was "Why..." :). I know that there is an explanation given by a member of hibernate team. I was not able to find it and that is why I asked.</p> <p>I want to find out the motivation of this design decision.</p> <p>(if you interested how I have faced this problem: I inherited a project built using hibernate 3. It was Jboss 4.0.something + hibernate was already there (you'd download it all together). I was moving this project to Jboss 4.2.2 and I found out that there are inherited mappings of "@OneToMany mappedBy" and it worked fine on old setup...)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload