views:

2238

answers:

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

A: 

I think you need to annotate your Problem super-class with @MappedSuperclass instead of @Entity.

Peter Hilton
A: 

That would be great if only I was not going to store objects of class Problem. But I want to store them.

Georgy Bolyuba
+1  A: 

Unfortunately, according to the Hibernate documentation "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.

In your case, you could define the following:

public interface Problem {
    public Person getPerson();
}

public interface UglyProblem extends Problem {
}

Then implement these interfaces using an abstract superclass and two entity subclasses:

@MappedSuperclass
public abstract class AbstractProblemImpl implements Problem {
    @ManyToOne
    private Person person;

    public Person getPerson() {
        return person;
    }
}

@Entity
public class ProblemImpl extends AbstractProblemImpl implements Problem {
}

@Entity
public class UglyProblemImpl extends AbstractProblemImpl implements UglyProblem {
}

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

David Crow
A: 

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.

Georgy Bolyuba
A: 

So the issue is with using @OneToMany mappedBy to a superclass. Have a look at this post. It addresses this specific problem and offers three options.

David Crow
A: 

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

Georgy Bolyuba
Try using the "add comment" link to do this sort of thing.
Rintoul
+3  A: 

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". Firts 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

Marcio Aguiar
A: 

I figured out how to do the OneToMany mappedBy problem.

In the derived class UglyProblem from the original post. The callback method needs to be in the derived class not the parent class.

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@ForceDiscriminator
public class Problem {

}

@Entity
@DiscriminatorValue("UP")
public class UglyProblem extends Problem {
    @ManyToOne
    private Person person;
}

@Entity
public class Person {
    @OneToMany(mappedBy="person")
    private List< UglyProblem > problems;
}

Found the secret sauce for using Hibernate at least. http://docs.jboss.org/hibernate/stable/annotations/api/org/hibernate/annotations/ForceDiscriminator.html The @ForceDiscriminator makes the @OneToMany honor the discriminator

Requires Hibernate Annotations.

Bill Leeper