`
joeblow
  • 浏览: 176095 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JPA中继承策略的重载

    博客分类:
  • JEE
阅读更多
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. A persistence provider does not need to support this feature, but the Java API specification allows for this. So to make sure that your code is portable across vendors, it's best to avoid using this feature.

----------------------------------------------------------------------------------------------
http://www.java-tips.org/java-ee-tips/enterprise-java-beans/inheritance-and-the-java-persistenc.html










分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics