【问题标题】:Inheritance in hibernate where derived class has no associated table派生类没有关联表的休眠中的继承
【发布时间】:2014-12-11 11:15:50
【问题描述】:

我正在为需要表示报告的应用程序编写实体,所有这些报告都有一些公共字段,例如;报告名称、期间等。所有这些报告都包含具有不同列的表格(您可以将其视为 Excel 表中的表示形式)。到目前为止,我已经设计了我的数据库表,其中所有报告的公共字段将存储在父表中,并且对于每个报告,将有另一个包含多个表的表来存储每个报告特定的数据。在数据库方面,这种设计对我来说很好。 在为休眠创建实体时,我想创建一个包含公共字段的父报表类。此报表类必须由所有特定报表类继承,并且将在派生类中定义更多字段,这些字段将特定于每个报表。这些字段将映射到子表中的列。 我研究了单表继承,但问题是我无法定义任何分隔符,@MappedSuperclass 似乎也不是这里的解决方案,因为我的父类需要映射到表。

实体类必须如下所示;

public class Report // maps to parent table
{
  private Integer reportId;
  private String reportName;
}

public class ReportAData // maps to child table
{
  private Date column1;
  private String column2;
}

public class ReportA extends Report // does not map to any table
{
  private list<ReportARecord> records;
}

谁能建议我如何做到这一点。

【问题讨论】:

    标签: java hibernate jpa


    【解决方案1】:

    TABLE_PER_CLASS 继承策略听起来像你想要的。以this blog post 为例。

    基本上,您将拥有一个用于Report 的表,以及一个用于每个子类的表,该表将具有指向Report 表和所有其他特定数据的外键。

    【讨论】:

    • 但是我没有任何子类表,创建子类的目的只是因为每个子类都有一些不同类型的组合对象,通常是一个列表。
    • 您写了where the common fields for all the reports will be stored in a parent table, and for each report there will be another table of multiple tables which will store the data specific to each report,据我了解您对每种报告类型都有一个表格。
    • 另外,你能解释一下the problem is that i can not define any delimiter的意思吗?
    【解决方案2】:

    我使用@Entity 管理它,即使类没有表:

    @Entity
    @Table(name = "REPORT")
    @Inheritance(strategy = InheritanceType.JOINED)
    public class Report // maps to parent table
    {
      private Integer reportId;
      private String reportName;
    }
    
    @Entity
    public class ReportA extends Report // does not map to any table
    {
      private list<ReportARecord> records;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-29
      • 2018-03-05
      • 1970-01-01
      • 2011-03-30
      • 2019-07-05
      • 2016-10-04
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多