<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="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>

[{'answer_id': 38096, 'author': 'Peter Hilton', 'author_id': 2670, 'author_profile': 'https://Stackoverflow.com/users/2670', 'pm_score': 1, 'selected': False, 'text': '<p>I think you need to annotate your <em>Problem</em> super-class with <em><a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/MappedSuperclass.html" rel="nofollow noreferrer">@MappedSuperclass</a></em> instead of <em>@Entity</em>.</p>\n'}, {'answer_id': 38992, 'author': 'David Crow', 'author_id': 2783, 'author_profile': 'https://Stackoverflow.com/users/2783', 'pm_score': 3, 'selected': False, 'text': '<p>Unfortunately, according to the Hibernate <a href="http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#d0e939" rel="noreferrer">documentation</a> "Properties from superclasses not mapped as @MappedSuperclass are ignored." I ran up against this too. My solution was to represent the desired inheritance through interfaces rather than the entity beans themselves.</p>\n\n<p>In your case, you could define the following:</p>\n\n<pre><code>public interface Problem {\n    public Person getPerson();\n}\n\npublic interface UglyProblem extends Problem {\n}\n</code></pre>\n\n<p>Then implement these interfaces using an abstract superclass and two entity subclasses:</p>\n\n<pre><code>@MappedSuperclass\npublic abstract class AbstractProblemImpl implements Problem {\n    @ManyToOne\n    private Person person;\n\n    public Person getPerson() {\n        return person;\n    }\n}\n\n@Entity\npublic class ProblemImpl extends AbstractProblemImpl implements Problem {\n}\n\n@Entity\npublic class UglyProblemImpl extends AbstractProblemImpl implements UglyProblem {\n}\n</code></pre>\n\n<p>As an added benefit, if you code using the interfaces rather than the actual entity beans that implement those interfaces, it makes it easier to change the underlying mappings later on (less risk of breaking compatibility).</p>\n'}, {'answer_id': 53448, 'author': 'Marcio Aguiar', 'author_id': 4213, 'author_profile': 'https://Stackoverflow.com/users/4213', 'pm_score': 4, 'selected': True, 'text': '<p>I think it\'s a wise decision made by the Hibernate team. They could be less arrogante and make it clear why it was implemented this way, but that\'s just how Emmanuel, Chris and Gavin works. :)</p>\n\n<p>Let\'s try to understand the problem. I think your concepts are "lying". First you say that many <strong>Problem</strong>s are associated to <strong>People</strong>. But, then you say that one <strong>Person</strong> have many <strong>UglyProblem</strong>s (and does not relate to other <strong>Problem</strong>s). Something is wrong with that design.</p>\n\n<p>Imagine how it\'s going to be mapped to the database. You have a single table inheritance, so:</p>\n\n<pre><code>          _____________\n          |__PROBLEMS__|          |__PEOPLE__|\n          |id &lt;PK&gt;     |          |          |\n          |person &lt;FK&gt; | --------&gt;|          |\n          |problemType |          |_________ |\n          -------------- \n</code></pre>\n\n<p>How is hibernate going to enforce the database to make <strong>Problem</strong> only relate to <strong>People</strong> if its <strong>problemType</strong> is equal UP? That\'s a very difficult problem to solve. So, if you want this kind of relation, every subclass must be in it\'s own table. That\'s what <code>@MappedSuperclass</code> does.</p>\n\n<p>PS.: Sorry for the ugly drawing :D</p>\n'}, {'answer_id': 1923659, 'author': 'Bill Leeper', 'author_id': 234014, 'author_profile': 'https://Stackoverflow.com/users/234014', 'pm_score': 1, 'selected': False, 'text': '<p>I figured out how to do the OneToMany mappedBy problem. </p>\n\n<p>In the derived class UglyProblem from the original post.  The callback method needs to be in the derived class not the parent class.</p>\n\n<pre><code>@Entity\n@Inheritance(strategy = InheritanceType.SINGLE_TABLE)\n@ForceDiscriminator\npublic class Problem {\n\n}\n\n@Entity\n@DiscriminatorValue("UP")\npublic class UglyProblem extends Problem {\n    @ManyToOne\n    private Person person;\n}\n\n@Entity\npublic class Person {\n    @OneToMany(mappedBy="person")\n    private List&lt; UglyProblem &gt; problems;\n}\n</code></pre>\n\n<p>Found the secret sauce for using Hibernate at least.  <a href="http://docs.jboss.org/hibernate/stable/annotations/api/org/hibernate/annotations/ForceDiscriminator.html" rel="nofollow noreferrer">http://docs.jboss.org/hibernate/stable/annotations/api/org/hibernate/annotations/ForceDiscriminator.html</a>  The @ForceDiscriminator makes the @OneToMany honor the discriminator</p>\n\n<p>Requires Hibernate Annotations.</p>\n'}, {'answer_id': 26091090, 'author': 'Guillaume Carre', 'author_id': 1084836, 'author_profile': 'https://Stackoverflow.com/users/1084836', 'pm_score': 3, 'selected': False, 'text': '<p>In my case I wanted to use the SINGLE_TABLE inheritance type, so using @MappedSuperclass wasn\'t an option.</p>\n\n<p>What works, although not very clean, is to add the Hibernate proprietary <a href="http://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/annotations/Where.html" rel="noreferrer">@Where</a> clause to the @OneToMany association to force the type in queries:</p>\n\n<pre class="lang-java prettyprint-override"><code>@OneToMany(mappedBy="person")\n@Where(clause="DTYPE=\'UP\'")\nprivate List&lt; UglyProblem &gt; problems;\n</code></pre>\n'}, {'answer_id': 62408017, 'author': 'Sašo Horvat', 'author_id': 13755527, 'author_profile': 'https://Stackoverflow.com/users/13755527', 'pm_score': -1, 'selected': False, 'text': '<p>In my opinion @JoinColumn should at least provide an option to apply the @DiscriminatorColumn = @DiscriminatorValue to the SQL "where" clause, although I would prefer this behaviour to be a default one.</p>\n\n<p>I am very surprised that in the year 2020 this is still an issue.\nSince this object design pattern is not so rare, I think it is a disgrace for JPA not yet covering this simple feature in the specs, thus still forcing us to search for ugly workarounds.</p>\n\n<p>Why must this be so difficult? It is just an additional where clause and yes, I do have a db index prepared for @JoinColumn, @DiscriminatorColumn combo.</p>\n\n<p>.i.. JPA</p>\n\n<p>Introduce your own custom annotations and write code that generates native queries. It will be a good exercise.</p>\n'}]
['https://Stackoverflow.com/questions/38057', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4052/']