Hibernate:是否可以将多级继承映射到单表?
我有以下继承层次结构:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,我已经添加了鉴别器列,现在它可以工作了。
更改的代码:(
我添加它只是为了提供一个可接受的答案——如果没有对问题的有用评论,我不会解决它。)
OK, I've added discriminator column and now it works.
Changed code:
(I'm adding it just to provide an accepted answer -- I wouldn't resolve it without the helpful comments to the question.)
接受的答案几乎是完美的。为了使它更清楚,我想向每个继承级别添加一个
@DiscriminatorValue
。物化表看起来像
The accepted answer is almost perfect. To make it more clear I want to add a
@DiscriminatorValue
to each inheritance level.And the materiliazed table looks like
尝试 @MappedSuperclass 注释:
Try the @MappedSuperclass annotation :