单表继承查询

时间:2011-08-12 06:56:49

标签: jpa single-table-inheritance

我有一个TransactionLogs的现有表,它可以链接到External或InternalType。与现金调整相对应的ID游戏事务存储在名为事务ID 的单个列中,另一个名为 type 的列表示它链接到哪个表

由于现有表的性质,我将其映射到单个表继承:

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.INTEGER)
public class TransLog implements Serializable {
    @Id
    @GeneratedValue
    private Long id;
    private Integer type;
// getters and setters
}


@Entity
public class InternalAdjustmentTransLog extends TransLog {
    @ManyToOne
    @JoinColumn(name = "TransID", nullable = false)
    private InternalAdjustmentRecord internalAdjustmentRecord;
    // getters and setters
}

@Entity
public class ExternalTransLog extends TransLog {
    @ManyToOne
    @JoinColumn(name = "TransID", nullable = false)
    private ExternalAdjustmentRecord externalAdjustmentRecord;
}

这两个子类中的每一个都有其定义的描述符值的子类。

通过上面给出的设置,有些实例我需要获得两者的统一数据 内部和外部记录。完成此任务的最佳方法是什么?起初我认为使用TransLog作为查询的根类就足够了(我使用的是jpa标准)。但是,我需要获得TransId(它们在子类中定义并指向2个不相关的不同对象)。

感谢。

1 个答案:

答案 0 :(得分:0)

您可以在TransLog中创建抽象方法,返回您需要的内容并在两个子类中实现它。