Why @OneToMany doesn't 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'm trying to do. I expect the @ManyToOne person to be inherited by the UglyProblem class. But there will be an exception saying something like: "There is no such property (mappedBy =" person ") in the UglyProblem class."

All I found is this . I could not find a message from Emmanuel Bernard explaining the reasons for this.


Unfortunately, according to the Hibernate documentation, "Properties from superclasses not displayed as @MappedSuperclass are ignored."

Well, I think it means that if I have these two classes:

public class A {
    private int foo;
}

@Entity
public class B extens A {
}

then the field foowill not be displayed for class B. This 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;
}
}

, UglyProblem id name, . (, , , ). :

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;

:

, @ManyToOne UglyProblem.

, , , ManyToOne.


, . , . : "...":). , , . , .

.

( , : , hibernate 3. Jboss 4.0.something + hibernate ( ). Jboss 4.2.2, , "@OneToMany mappedBy", ...)

+5
5

, , Hibernate. arrogante , , , , . :)

. , "". , . , UglyProblem s ( s). - .

, . , :

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

hibernate , , Type UP? . , , . , @MappedSuperclass.

PS: : D

+4

SINGLE_TABLE, @MappedSuperclass .

, , , @Where @OneToMany,

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

, Hibernate , @MappedSuperclass, ." . , beans.

:

public interface Problem {
    public Person getPerson();
}

public interface UglyProblem extends Problem {
}

:

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

, , beans, , ( ).

+4
+1

, OneToMany mappedBy.

UglyProblem . , .

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

Hibernate . http://docs.jboss.org/hibernate/stable/annotations/api/org/hibernate/annotations/ForceDiscriminator.html @ForceDiscriminator @OneToMany

Hibernate.

0

All Articles