• JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-003Table per concrete class with unions(@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)、<union-subclass>)


    一、代码

    1.

     1 package org.jpwh.model.inheritance.tableperclass;
     2 
     3 import org.jpwh.model.Constants;
     4 
     5 import javax.persistence.Entity;
     6 import javax.persistence.GeneratedValue;
     7 import javax.persistence.Id;
     8 import javax.persistence.Inheritance;
     9 import javax.persistence.InheritanceType;
    10 import javax.validation.constraints.NotNull;
    11 
    12 @Entity
    13 //in Hibernate, it’s equivalent to a  <union-subclass> mapping in the old native Hibernate  XML metadata
    14 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
    15 public abstract class BillingDetails {
    16 
    17     @Id
    18     @GeneratedValue(generator = Constants.ID_GENERATOR)
    19     protected Long id;
    20 
    21     @NotNull
    22     protected String owner;
    23 
    24     // ...
    25 
    26     protected BillingDetails() {
    27     }
    28 
    29     protected BillingDetails(String owner) {
    30         this.owner = owner;
    31     }
    32 
    33     public Long getId() {
    34         return id;
    35     }
    36 
    37     public String getOwner() {
    38         return owner;
    39     }
    40 
    41     public void setOwner(String owner) {
    42         this.owner = owner;
    43     }
    44 }

    2.

     1 package org.jpwh.model.inheritance.tableperclass;
     2 
     3 import javax.persistence.Entity;
     4 import javax.validation.constraints.NotNull;
     5 
     6 @Entity
     7 public class CreditCard extends BillingDetails {
     8 
     9     @NotNull
    10     protected String cardNumber;
    11 
    12     @NotNull
    13     protected String expMonth;
    14 
    15     @NotNull
    16     protected String expYear;
    17 
    18     // ...
    19 
    20     public CreditCard() {
    21         super();
    22     }
    23 
    24     public CreditCard(String owner, String cardNumber, String expMonth, String expYear) {
    25         super(owner);
    26         this.cardNumber = cardNumber;
    27         this.expMonth = expMonth;
    28         this.expYear = expYear;
    29     }
    30 
    31     public String getCardNumber() {
    32         return cardNumber;
    33     }
    34 
    35     public void setCardNumber(String cardNumber) {
    36         this.cardNumber = cardNumber;
    37     }
    38 
    39     public String getExpMonth() {
    40         return expMonth;
    41     }
    42 
    43     public void setExpMonth(String expMonth) {
    44         this.expMonth = expMonth;
    45     }
    46 
    47     public String getExpYear() {
    48         return expYear;
    49     }
    50 
    51     public void setExpYear(String expYear) {
    52         this.expYear = expYear;
    53     }
    54 }

    3.

     1 package org.jpwh.model.inheritance.tableperclass;
     2 
     3 import javax.persistence.Entity;
     4 import javax.validation.constraints.NotNull;
     5 
     6 @Entity
     7 public class BankAccount extends BillingDetails {
     8 
     9     @NotNull
    10     protected String account;
    11 
    12     @NotNull
    13     protected String bankname;
    14 
    15     @NotNull
    16     protected String swift;
    17 
    18     public BankAccount() {
    19         super();
    20     }
    21 
    22     public BankAccount(String owner, String account, String bankname, String swift) {
    23         super(owner);
    24         this.account = account;
    25         this.bankname = bankname;
    26         this.swift = swift;
    27     }
    28 
    29     public String getAccount() {
    30         return account;
    31     }
    32 
    33     public void setAccount(String account) {
    34         this.account = account;
    35     }
    36 
    37     public String getBankname() {
    38         return bankname;
    39     }
    40 
    41     public void setBankname(String bankname) {
    42         this.bankname = bankname;
    43     }
    44 
    45     public String getSwift() {
    46         return swift;
    47     }
    48 
    49     public void setSwift(String swift) {
    50         this.swift = swift;
    51     }
    52 }

    二、相比上一个方法的优点

    1.查询父类不用再通过多次select,而是通过union all,the query select bd from BillingDetails bd generates the following SQL statement:

     1 elect
     2 ID, OWNER, EXPMONTH, EXPYEAR, CARDNUMBER,
     3 ACCOUNT, BANKNAME, SWIFT, CLAZZ_
     4 from
     5     (select ID, OWNER, EXPMONTH, EXPYEAR, CARDNUMBER,
     6         null as ACCOUNT,
     7         null as BANKNAME,
     8         null as SWIFT,
     9         1 as CLAZZ_ 
    10         from CREDITCARD 
    11         union all 
    12     select id, OWNER,
    13         null as EXPMONTH,
    14         null as EXPYEAR,
    15         null as CARDNUMBER,
    16         ACCOUNT, BANKNAME, SWIFT,
    17         2 as CLAZZ_ from BANKACCOUNT
    18     ) as BILLINGDETAILS

    2.Another much more important advantage is the ability to handle polymorphic associations; for example, an association mapping from User to BillingDetails would now be possible. Hibernate can use a UNION query to simulate a single table as the target of the association mapping.

  • 相关阅读:
    Button与Submit调用前台与后台代码的方法
    ASP.NET前台代码绑定后台变量方法总结
    点击后禁用提交按钮
    SilverLight3测试(1): Message: AG_E_NETWORK_ERROR
    SilverLight 问题解决
    XmlSerializers.dll.deploy 未成功
    命令模式泛型?问题?
    SilverLight 收藏(2009年3月27日)
    在Web安装工程中为虚拟目录指定不同的本地路径
    Silverlight 3 Sample Application: Slidentity
  • 原文地址:https://www.cnblogs.com/shamgod/p/5365465.html
Copyright © 2020-2023  润新知