JPA:如何扩展类并更改表映射?

时间:2022-08-22 21:17:22

I have created a basic set of classes with methods for doing accounting.

我创建了一个基本的类集,其中包含用于进行记帐的方法。

My Ledger class allows you to maintain a basic ledger which allows you to obtain the current balance, and to perform transactions on the ledger that adds a new LedgeEntry.

我的Ledger类允许您维护一个基本分类帐,它允许您获取当前余额,并在分类帐上执行添加新LedgeEntry的事务。

I have this generic ledger system working well.

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

Insider Ledger is my LedgerEntry collection which is a List entries.

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

Now, I want to use this to create Billing / BillingEntry entities (which map to say BILLING and BILLING_ENTRIES table) and CustomerCredits / CustomerCreditsEntry entites (mapped to CUST_CREDITS; CUST_CREDITS_ENTRY).

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

I would like to create these classes by extending from Ledger and LedgerEntry. The parent entity class (Ledger, Billing, CustomerCredits) can be easily mapped to a different table using the @Table annotation.

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

But I'm not sure how to handle the replacing the LedgerEntry entities with their corresponding BillingEntry, CustomerCreditsEntry.

但我不知道如何处理用相应的BillingEntry,CustomerCreditsEntry替换LedgerEntry实体。


Edit 2010-09-24:

I just gave up and am having all Ledger-derviced entites go to child LedgerEntry entities (for now).

我只是放弃了,所有Ledger-derviced entites都会转到孩子LedgerEntry实体(现在)。

The problem here, I think, is that I can't make methods in a parent class that refer to objects that would only exist in the inherited class?

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

1 个解决方案

#1


0  

It sounds like the purrrfect scenario for generics :)

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

I imagine something like:

我想象的是:

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

public class CustomerCreditsLedger extends Ledger<CustomerCreditsEntry> {
}

And probably for wiring up JPA with the table inheritance take a look at: http://www.jpox.org/docs/1_2/jpa_orm/inheritance.html (it is old, but seems to be correct and figuring the annotation-based config out is not that hard). From what I read you should be looking at JOINED or TABLE_PER_CLASS strategies.

并且可能用JPA连接表继承来看看:http://www.jpox.org/docs/1_2/jpa_orm/inheritance.html(它是旧的,但似乎是正确的并且基于注释计算配置不是那么难)。根据我的阅读,你应该看看JOINED或TABLE_PER_CLASS策略。

#1


0  

It sounds like the purrrfect scenario for generics :)

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

I imagine something like:

我想象的是:

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

public class CustomerCreditsLedger extends Ledger<CustomerCreditsEntry> {
}

And probably for wiring up JPA with the table inheritance take a look at: http://www.jpox.org/docs/1_2/jpa_orm/inheritance.html (it is old, but seems to be correct and figuring the annotation-based config out is not that hard). From what I read you should be looking at JOINED or TABLE_PER_CLASS strategies.

并且可能用JPA连接表继承来看看:http://www.jpox.org/docs/1_2/jpa_orm/inheritance.html(它是旧的,但似乎是正确的并且基于注释计算配置不是那么难)。根据我的阅读,你应该看看JOINED或TABLE_PER_CLASS策略。