Hibernate:是否可以将多级继承映射到单表?

发布于 2025-01-02 00:09:32 字数 657 浏览 0 评论 0原文

我有以下继承层次结构:

Task
  |
SpecificTask
  |
VerySpecificTask

我想使用单表继承来持久化它,所以我注释了类:

@Entity
@Table(name="task")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Task 

@Entity
public class SpecificTask extends Task 

@Entity
public class VerySpecificTask extends SpecificTask

当我尝试保存 VerySpecificTask 类的对象时,出现错误:

Unable to resolve entity name from Class [com.application.task.VerySpecificTask] 
expected instance/subclass of [com.application.task.Task]

我做错了什么?是否可以将多级继承映射到单表?

编辑:这是一个蹩脚的错误,我很快就解决了,所以我删除了它以免弄乱这个问题。

I have following inheritance hierarchy:

Task
  |
SpecificTask
  |
VerySpecificTask

And I'd like to persist it usign single-table inheritance, so I annotated classes:

@Entity
@Table(name="task")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Task 

@Entity
public class SpecificTask extends Task 

@Entity
public class VerySpecificTask extends SpecificTask

When I try to save an object of VerySpecificTask class, I get an error:

Unable to resolve entity name from Class [com.application.task.VerySpecificTask] 
expected instance/subclass of [com.application.task.Task]

What do I wrong? Is it possible to map multi-level inheritance to single table?

EDIT: Here was a lame bug, I've resolved quickly, so I deleted it to not mess this question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怀念你的温柔 2025-01-09 00:09:32

好的,我已经添加了鉴别器列,现在它可以工作了。
更改的代码:(

@Entity
@Table(name="task")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
        name="DTYPE",
        discriminatorType=DiscriminatorType.STRING
    )

@Entity
public class SpecificTask extends Task 

@Entity
public class VerySpecificTask extends SpecificTask

我添加它只是为了提供一个可接受的答案——如果没有对问题的有用评论,我不会解决它。)

OK, I've added discriminator column and now it works.
Changed code:

@Entity
@Table(name="task")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
        name="DTYPE",
        discriminatorType=DiscriminatorType.STRING
    )

@Entity
public class SpecificTask extends Task 

@Entity
public class VerySpecificTask extends SpecificTask

(I'm adding it just to provide an accepted answer -- I wouldn't resolve it without the helpful comments to the question.)

跨年 2025-01-09 00:09:32

接受的答案几乎是完美的。为了使它更清楚,我想向每个继承级别添加一个 @DiscriminatorValue

@Entity
@Table(name="task")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
        name="DTYPE",
        discriminatorType=DiscriminatorType.STRING
)
public class Task

---

@Entity
@DiscriminatorValue(value="DS")
public class SpecificTask extends Task 

---

@Entity
@DiscriminatorValue(value="DV")
public class VerySpecificTask extends SpecificTask

物化表看起来像

--------------- 
Table: task
---------------
|...|DTYPE|...|
---------------
|...|DS   |...|
|...|DV   |...|
|...|DS   |...|
...

The accepted answer is almost perfect. To make it more clear I want to add a @DiscriminatorValue to each inheritance level.

@Entity
@Table(name="task")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
        name="DTYPE",
        discriminatorType=DiscriminatorType.STRING
)
public class Task

---

@Entity
@DiscriminatorValue(value="DS")
public class SpecificTask extends Task 

---

@Entity
@DiscriminatorValue(value="DV")
public class VerySpecificTask extends SpecificTask

And the materiliazed table looks like

--------------- 
Table: task
---------------
|...|DTYPE|...|
---------------
|...|DS   |...|
|...|DV   |...|
|...|DS   |...|
...
烂柯人 2025-01-09 00:09:32

尝试 @MappedSuperclass 注释:

@MappedSuperclass
public class BaseEntity {
    @Basic
    @Temporal(TemporalType.TIMESTAMP)
    public Date getLastUpdate() { ... }
    public String getLastUpdater() { ... }
    ...
}

@Entity 
public class Order extends BaseEntity {
    @Id public Integer getId() { ... }
    ...
}

在数据库中,此层次结构将表示为具有 id、lastUpdate 和 lastUpdater 列的 Order 表。嵌入的超类属性映射被复制到其实体子类中。请记住,可嵌入超类并不是层次结构的根。

Try the @MappedSuperclass annotation :

@MappedSuperclass
public class BaseEntity {
    @Basic
    @Temporal(TemporalType.TIMESTAMP)
    public Date getLastUpdate() { ... }
    public String getLastUpdater() { ... }
    ...
}

@Entity 
public class Order extends BaseEntity {
    @Id public Integer getId() { ... }
    ...
}

In database, this hierarchy will be represented as an Order table having the id, lastUpdate and lastUpdater columns. The embedded superclass property mappings are copied into their entity subclasses. Remember that the embeddable superclass is not the root of the hierarchy though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文