【问题标题】:JPA: how can I extend classes and change the table mapping?JPA:如何扩展类并更改表映射?
【发布时间】:2011-04-14 10:48:06
【问题描述】:

我创建了一组基本类,其中包含会计方法。

My Ledger 类允许您维护一个基本分类帐,该分类帐允许您获取当前余额,并在添加新 LedgeEntry 的分类帐上执行交易。

我的这个通用分类帐系统运行良好。

Insider Ledger 是我的 LedgerEntry 集合,它是一个列表条目。

现在,我想用它来创建 Billing / BillingEntry 实体(映射到 BILLING 和 BILLING_ENTRIES 表)和 CustomerCredits / CustomerCreditsEntry 实体(映射到 CUST_CREDITS; CUST_CREDITS_ENTRY)。

我想通过从 Ledger 和 LedgerEntry 扩展来创建这些类。父实体类(Ledger、Billing、CustomerCredits)可以使用@Table 注解轻松映射到不同的表。

但我不确定如何处理将 LedgerEntry 实体替换为相应的 BillingEntry、CustomerCreditsEntry。


编辑 2010-09-24:

我刚刚放弃,现在让所有 Ledger 衍生实体转到子 LedgerEntry 实体。

我认为这里的问题是我不能在父类中创建方法来引用仅存在于继承类中的对象?

【问题讨论】:

    标签: java jpa mapping


    【解决方案1】:

    这听起来像是泛型的完美场景:)

    我想像这样:

    public class Ledger<T extends LedgerEntry> {
       List<T> entries;
       // getters and setters
    }
    
    public class CustomerCreditsLedger extends Ledger<CustomerCreditsEntry> {
    }
    

    对于使用表继承连接 JPA,请查看:http://www.jpox.org/docs/1_2/jpa_orm/inheritance.html(它很旧,但似乎是正确的,并且弄清楚基于注释的配置并不难)。根据我读到的内容,您应该查看 JOINED 或 TABLE_PER_CLASS 策略。

    【讨论】:

    • 你不能用 JPA(通用集合)映射这样的模型。
    • 我正在使用 JOINED 方法为继承 Ledger 的不同实体实例化不同的表。这很好用。问题是我真正想要的是为从 LedgerEntry 继承的子实体实例化不同的表......
    猜你喜欢
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多