JPA中继承策略的重载

JPA规范中规定了三种映射继承的关系策略,分别如下所示。

●继承关系的实体保存在一个表(Single Table per Class Hierarchy Strategy)
●每个子类实体保存在一个表(Joined Subclass Strategy)
●每个实体类保存在一个表(Table per Class Strategy)

那么同一个对象树上是否可以有两个或多个继承策略呢?(即是否可以对继承策略进行重载)

答案要问JPA Vendor了,在java-tips.org找到下面一段:

----------------------------------------------------------------------------------------------

It's possible for an entity to specify a different inheritance strategy for its subclasses. The BrokerageAccount entity illustrates that.


@Entity
@Inheritance(strategy=InheritanceStrategy.JOINED)
public class BrokerageAccount extends Account{

float tradeFees;

}

@Entity
public class MarginAccount extends BrokerageAccount{

float maxLoanAllowed;

}


Because the Account entity specifies a SINGLE_TABLE per class hierarchy strategy, Account, CheckingAccount, SavingsAccount, CreditCardAccount and BrokerageAccount are stored in the same database table. However the MarginAccount entity's maxLoanAllowed attribute is stored in a separate table because the inheritance strategy was changed to "JOINED" in the BrokerageAccount. [color=red][b]A persistence provider does not need to support this feature, but the Java API specification allows for this.[/b][/color] So to make sure that your code is portable across vendors, it's best to avoid using this feature.

----------------------------------------------------------------------------------------------
[url]http://www.java-tips.org/java-ee-tips/enterprise-java-beans/inheritance-and-the-java-persistenc.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值