@MappedSuperclass 和实现 table
@MappedSuperclass and implementation table
我继承了一些非常糟糕的代码,我希望对其进行重构以提高可重用性。有一组报告 table,主要由 3 列组成:id
、report_type_fk
和 report_description
。我想将所有报告 table 合并为一个以便于使用。
我正在重构代码,我认为最好将我们当前的实体分解,这样 Report
是一个具有 type
实现的抽象 class。例如DmvReport extends Report
、CreditScoreReport extends Report
等
我 运行 遇到的问题是只有 1 个报告 table 所有实体都需要保存到。有没有办法让 abstract Report
对象的所有具体实现都保存到同一个 table?
这是我继承的错误代码的示例
举报class
@Entity
@Table(name = "report")
public class Report<E extends Exception> {
private long id;
private ReportType type;
private String description;
...
...
}
信用报告class
@Entity
@Table(name = "credit_report")
public class CreditScore Report<E extends Exception> extends Report<E> {
private long id;
private ReportType type;
private String description;
...
...
}
我想把它变成:
@MappedSuperclass
@Table(name = "report")
public abstract class Report<E extends Exception> {
@Id @Column(name="id")
private long id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "report_type_id")
private ReportType type;
@column(name="description")
private String description;
...
...
}
@Entity
@Table(name = "report")
public class CreditScoreReport<E extends Exception> extends Report<E> {
public void doCreditScoreStuff(){
...
}
}
@Entity
@Table(name = "report")
public class DmvReport<E extends Exception> extends Report<E> {
public void doDmvStuff(){
...
}
}
我认为你应该使用 @Inheritance
而不是 @MappedSuperClass
。您的代码将如下所示:
@Entity
@Table(name = "report")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "report_type_id", discriminatorType = DiscriminatorType.INTEGER)
public abstract class Report<E extends Exception> {
@Id @Column(name="id")
private long id;
@column(name="description")
private String description;
...
...
}
@Entity(name = "CreditScoreReport")
@DiscriminatorValue("1") // the id corresponding to the credit score report
public class CreditScoreReport<E extends Exception> extends Report<E> {
@Column(name = "specific_credit_score_report_1)
private Integer specificCreditScoreReport1;
public void doCreditScoreStuff(){
...
}
}
@Entity(name = "DmvReport")
@DiscriminatorValue("2") // the id corresponding to the DMV report
public class DmvReport<E extends Exception> extends Report<E> {
@Column(name = "specific_dmv_score_report_1)
private Integer specificDmvScoreReport1;
public void doDmvStuff(){
...
}
}
此策略允许您将信用评分报告和 DMV 报告数据存储在一个 table (report
) 中,但根据 report_value_id
字段实例化适当的实体。您不必在参数中定义 report_value_id
,因为它已用于创建所需的实体。
这是您要找的吗?
我继承了一些非常糟糕的代码,我希望对其进行重构以提高可重用性。有一组报告 table,主要由 3 列组成:id
、report_type_fk
和 report_description
。我想将所有报告 table 合并为一个以便于使用。
我正在重构代码,我认为最好将我们当前的实体分解,这样 Report
是一个具有 type
实现的抽象 class。例如DmvReport extends Report
、CreditScoreReport extends Report
等
我 运行 遇到的问题是只有 1 个报告 table 所有实体都需要保存到。有没有办法让 abstract Report
对象的所有具体实现都保存到同一个 table?
这是我继承的错误代码的示例
举报class
@Entity
@Table(name = "report")
public class Report<E extends Exception> {
private long id;
private ReportType type;
private String description;
...
...
}
信用报告class
@Entity
@Table(name = "credit_report")
public class CreditScore Report<E extends Exception> extends Report<E> {
private long id;
private ReportType type;
private String description;
...
...
}
我想把它变成:
@MappedSuperclass
@Table(name = "report")
public abstract class Report<E extends Exception> {
@Id @Column(name="id")
private long id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "report_type_id")
private ReportType type;
@column(name="description")
private String description;
...
...
}
@Entity
@Table(name = "report")
public class CreditScoreReport<E extends Exception> extends Report<E> {
public void doCreditScoreStuff(){
...
}
}
@Entity
@Table(name = "report")
public class DmvReport<E extends Exception> extends Report<E> {
public void doDmvStuff(){
...
}
}
我认为你应该使用 @Inheritance
而不是 @MappedSuperClass
。您的代码将如下所示:
@Entity
@Table(name = "report")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "report_type_id", discriminatorType = DiscriminatorType.INTEGER)
public abstract class Report<E extends Exception> {
@Id @Column(name="id")
private long id;
@column(name="description")
private String description;
...
...
}
@Entity(name = "CreditScoreReport")
@DiscriminatorValue("1") // the id corresponding to the credit score report
public class CreditScoreReport<E extends Exception> extends Report<E> {
@Column(name = "specific_credit_score_report_1)
private Integer specificCreditScoreReport1;
public void doCreditScoreStuff(){
...
}
}
@Entity(name = "DmvReport")
@DiscriminatorValue("2") // the id corresponding to the DMV report
public class DmvReport<E extends Exception> extends Report<E> {
@Column(name = "specific_dmv_score_report_1)
private Integer specificDmvScoreReport1;
public void doDmvStuff(){
...
}
}
此策略允许您将信用评分报告和 DMV 报告数据存储在一个 table (report
) 中,但根据 report_value_id
字段实例化适当的实体。您不必在参数中定义 report_value_id
,因为它已用于创建所需的实体。
这是您要找的吗?