如何在休眠中映射多级多态一对多实体?
我有一个这样的模型:
class Group {
@Id
@GeneratedValue
Long id
@OneToMany()
@JoinColumn()
List<Base> baseClasses;
}
这是基类:
@MappedSuperclass //tried to add strategy as well table per super class as well but it does not work
abstract class Base{
//some fields here
}
基类有两个级别的子级:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
class FirstLevel extends Base{
@Id
@GeneratedValue
Long id
//some fields here
}
@Entity
class SecondLevel extends FirstLevel{
@Id
@GeneratedValue
Long id
//some fields here
}
我正在使用 spring 数据(休眠)。我想要实现的是将这个类层次结构映射到数据库结构中,如下所示:
- table Group 具有对特定基类的一对多引用
- 特定基类 - SecondLevel 表
我想将这两个级别的 Base 层次结构映射到一个表中next 将可以通过组表内的 @OneToMany
映射来访问。
当我有 @MappedSuperclass
时,我收到以下错误:
是org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany 定位未映射的类:
当我用继承策略替换 MappedSuperclass 时 -> TABLE_PER_CLASS 表格 Hibernate 需要两个表 - FirstLevel 和 SecondLevel 我不想要。
总结一下 - 如何将多态关系 oneToMany 映射到多级继承层次结构?
I have a model like this:
class Group {
@Id
@GeneratedValue
Long id
@OneToMany()
@JoinColumn()
List<Base> baseClasses;
}
This is the base class:
@MappedSuperclass //tried to add strategy as well table per super class as well but it does not work
abstract class Base{
//some fields here
}
The base class has two levels of children:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
class FirstLevel extends Base{
@Id
@GeneratedValue
Long id
//some fields here
}
@Entity
class SecondLevel extends FirstLevel{
@Id
@GeneratedValue
Long id
//some fields here
}
I am using spring data (hibernate). What I would like to achieve is map this class hierarchy into database structure like this:
- table Group which has one to many reference to specific base class
- specific base class - SecondLevel table
I would like to map this two level of Base hierarchy into one table which next will be accessible through @OneToMany
mapping inside group table.
When I have @MappedSuperclass
I am getting the following error:
is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany
targeting an unmapped class:
When I replace MappedSuperclass with Inheritance strategy -> TABLE_PER_CLASS
Hibernate requires two tables - FirstLevel and SecondLevel which I dont want.
To sum up - How do I map polymorphic relation oneToMany to multiple level of inheritance hierarchy ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论