如何在休眠中映射多级多态一对多实体?

发布于 2025-01-18 12:21:26 字数 1231 浏览 1 评论 0原文

我有一个这样的模型:

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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文