为什么@OneToMany不能在Hibernate中继承 [英] Why @OneToMany does not work with inheritance in Hibernate

查看:111
本文介绍了为什么@OneToMany不能在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;
}

我认为我很清楚自己想要做什么。我期待@ManyToOne人被UglyProblem类继承。但是会有一个例外情况如下所示:UglyProblem类中找不到这样的属性(mappedBy =person)。

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

我发现的所有内容都是这个。我无法找到Emmanuel Bernard解释这个背后原因的帖子。




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


不幸的是,根据Hibernate文档超类的属性未映射为@MappedSuperclass忽略。

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

则字段 foo 不会映射到B类。这是有道理的。但如果我有这样的事情:

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

我期望UglyProblem类拥有fileds id name ,并且这两个类都使用同一个表进行映射。 (事实上​​,这正是发生了什么,我刚刚再次检查)。我有这个表:

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:


我期待@ManyToOne人被UglyProblem类继承。

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

我期望因为所有其他映射的字段是继承的,我没有看到任何理由为ManyToOne关系做出这个例外。

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

是的,我看到了。事实上,我为我的案例使用了只读解决方案。但我的问题是为什么......:)。我知道有一个由hibernate团队成员给出的解释。我无法找到它,这就是为什么我问。

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.

(如果您对我遇到过这个问题感兴趣,我继承了一个使用hibernate 3构建的项目。它是Jboss 4.0.something + hibernate已经存在了(您可以一起下载它)。这个项目到Jboss 4.2.2,我发现有@OneToMany mappedBy的继承映射,它在旧的设置上工作正常...)

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

推荐答案

我认为这是Hibernate团队做出的明智决定。他们可能不那么傲慢,并明确说明为什么这样实施,但这正是Emmanuel,Chris和Gavin的工作方式。 :)

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

让我们试着理解这个问题。我认为你的概念是撒谎。你说很多问题相关联。但是,那么你说一个有很多 UglyProblem (并且与其他问题无关)。这种设计有些问题。

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 |          |_________ |
          -------------- 

hibernate如何如果问题问题类型相等,则强制执行数据库以使问题仅与相关?这是一个非常难以解决的问题。所以,如果你想要这种关系,每个子类都必须在它自己的表中。这就是 @MappedSuperclass 所做的事情。

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 .:对不起,这张丑陋的图画:D

PS.: Sorry for the ugly drawing :D

这篇关于为什么@OneToMany不能在Hibernate中继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆