Why @OneToMany does not work with inheritance in Hibernate

Question :

Why @OneToMany does not work with inheritance in Hibernate,

Answer :

@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< UglyProblem > problems;  }  

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”)”.

All I found is  I was not able to find the post by Emmanuel Bernard explaining reasons behind this.

Unfortunately, according to the Hibernate documentation “Properties from superclasses not mapped as @MappedSuperclass are ignored.”

Well I think this means that if I have these two classes:

public class A {      private int foo;  }    @Entity  public class B extens A {  }  

then field foo will not be mapped for class B. Which makes sense. But if I have something like this:

@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;  }  }  

I expect the class UglyProblem to have fileds id and name 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:

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;  

Going back to my question:

I expect @ManyToOne person to be inherited by UglyProblem class.

I expect that because all other mapped fields are inherited and I do not see any reason to make this exception for ManyToOne relationships.

Read More  “Could not find type” error loading a form in the Windows Forms Designer

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.

I want to find out the motivation of this design decision.

(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…)

Read More  What SQL-server function can I use to get the character or byte length of a nvarchar(max) column?


In my case I wanted to use the SINGLE_TABLE inheritance type, so using @MappedSuperclass wasn’t an option.

What works, although not very clean, is to add the Hibernate proprietary  clause to the @OneToMany association to force the type in queries:

@OneToMany(mappedBy="person")  @Where(clause="DTYPE='UP'")  private List< UglyProblem > problems;  

That’s the answer Why @OneToMany does not work with inheritance in Hibernate, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Read More  Batch code indenters and beautifiers

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you