Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why @OneToMany does not work with inheritance in Hibernate

@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 this. 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.


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

like image 557
Georgy Bolyuba Avatar asked Sep 01 '08 15:09

Georgy Bolyuba


2 Answers

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 @Where clause to the @OneToMany association to force the type in queries:

@OneToMany(mappedBy="person")
@Where(clause="DTYPE='UP'")
private List< UglyProblem > problems;
like image 156
Guillaume Carre Avatar answered Oct 13 '22 00:10

Guillaume Carre


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

Let's try to understand the problem. I think your concepts are "lying". First you say that many Problems are associated to People. But, then you say that one Person have many UglyProblems (and does not relate to other Problems). Something is wrong with that design.

Imagine how it's going to be mapped to the database. You have a single table inheritance, so:

          _____________
          |__PROBLEMS__|          |__PEOPLE__|
          |id <PK>     |          |          |
          |person <FK> | -------->|          |
          |problemType |          |_________ |
          -------------- 

How is hibernate going to enforce the database to make Problem only relate to People if its problemType 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 @MappedSuperclass does.

PS.: Sorry for the ugly drawing :D

like image 31
Marcio Aguiar Avatar answered Oct 12 '22 22:10

Marcio Aguiar