JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-006Mixing inheritance strategies(@SecondaryTable、@PrimaryKeyJoinColumn、<join fetch="select">)

一、结构

For example, you can map a class hierarchy to a single table, but, for a particular subclass, switch to a separate table with a foreign key–mapping strategy, just as with table-per-subclass.

二、代码

1.

 1 package org.jpwh.model.inheritance.mixed;
 2 
 3 import org.jpwh.model.Constants;
 4 
 5 import javax.persistence.DiscriminatorColumn;
 6 import javax.persistence.Entity;
 7 import javax.persistence.GeneratedValue;
 8 import javax.persistence.Id;
 9 import javax.persistence.Inheritance;
10 import javax.persistence.InheritanceType;
11 import javax.validation.constraints.NotNull;
12 
13 @Entity
14 @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
15 @DiscriminatorColumn(name = "BD_TYPE")
16 public abstract class BillingDetails {
17 
18     @Id
19     @GeneratedValue(generator = Constants.ID_GENERATOR)
20     protected Long id;
21 
22     @NotNull
23     protected String owner;
24 
25     protected BillingDetails() {
26     }
27 
28     protected BillingDetails(String owner) {
29         this.owner = owner;
30     }
31 
32     public Long getId() {
33         return id;
34     }
35 
36     public String getOwner() {
37         return owner;
38     }
39 
40     public void setOwner(String owner) {
41         this.owner = owner;
42     }
43 }

 

2.

 1 package org.jpwh.model.inheritance.mixed;
 2 
 3 import javax.persistence.Column;
 4 import javax.persistence.DiscriminatorValue;
 5 import javax.persistence.Entity;
 6 import javax.persistence.PrimaryKeyJoinColumn;
 7 import javax.persistence.SecondaryTable;
 8 import javax.validation.constraints.NotNull;
 9 
10 @Entity
11 @DiscriminatorValue("CC")
12 @SecondaryTable(
13         name = "CREDITCARD",
14         pkJoinColumns = @PrimaryKeyJoinColumn(name = "CREDITCARD_ID")
15 )
16 public class CreditCard extends BillingDetails {
17 
18     @NotNull // Ignored by JPA for DDL, strategy is SINGLE_TABLE!
19     @Column(table = "CREDITCARD", nullable = false) // Override the primary table
20     protected String cardNumber;
21 
22     @Column(table = "CREDITCARD", nullable = false)
23     protected String expMonth;
24 
25     @Column(table = "CREDITCARD", nullable = false)
26     protected String expYear;
27 
28     // ...
29     public CreditCard() {
30         super();
31     }
32 
33     public CreditCard(String owner, String cardNumber, String expMonth, String expYear) {
34         super(owner);
35         this.cardNumber = cardNumber;
36         this.expMonth = expMonth;
37         this.expYear = expYear;
38     }
39 
40     public String getCardNumber() {
41         return cardNumber;
42     }
43 
44     public void setCardNumber(String cardNumber) {
45         this.cardNumber = cardNumber;
46     }
47 
48     public String getExpMonth() {
49         return expMonth;
50     }
51 
52     public void setExpMonth(String expMonth) {
53         this.expMonth = expMonth;
54     }
55 
56     public String getExpYear() {
57         return expYear;
58     }
59 
60     public void setExpYear(String expYear) {
61         this.expYear = expYear;
62     }
63 }

 

3.

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

 

4.At runtime, Hibernate executes an outer join to fetch BillingDetails and all sub-class instances polymorphically:

1 select
2     ID, OWNER, ACCOUNT, BANKNAME, SWIFT,
3     EXPMONTH, EXPYEAR, CARDNUMBER,
4     BD_TYPE
5 from
6     BILLINGDETAILS
7     left outer join CREDITCARD on ID=CREDITCARD_ID

 

5. If you have an exceptionally wide class hierarchy, the outer join can become a problem. Some data-base systems (Oracle, for example) limit the number of tables in an outer join operation. For a wide hierarchy, you may want to switch to a different fetching strategy that executes an immediate second SQL select instead of an outer join. 

 

 Switching the fetching strategy for this mapping isn’t available in JPA or Hibernate annotations at the time of writing, so you have to map the class in a native Hibernate XML file:

 

 

FetchSelect.hbm.xml

 

 

 1 <?xml version="1.0"?>
 2 <hibernate-mapping xmlns="http://www.hibernate.org/xsd/orm/hbm"
 3                    package="org.jpwh.model.inheritance.mixed"
 4                    default-access="field">
 5 
 6     <class name="BillingDetails"
 7            abstract="true">
 8         <id name="id">
 9             <generator class="native"/>
10         </id>
11         <discriminator column="BD_TYPE" type="string"/>
12         <property name="owner"
13                   not-null="true"/>
14 
15         <subclass name="CreditCard"
16                   discriminator-value="CC">
17             <join table="CREDITCARD" fetch="select">
18                 <key column="CREDITCARD_ID"/>
19                 <property name="cardNumber"
20                           column="CARDNUMBER"
21                           not-null="true"/>
22 
23                 <property name="expMonth"
24                           column="EXPMONTH"
25                           not-null="true"/>
26                 <property name="expYear"
27                           column="EXPYEAR"
28                           not-null="true"/>
29             </join>
30         </subclass>
31 
32         <subclass name="BankAccount"
33                   discriminator-value="BA">
34             <property name="account"
35                       not-null="false"/>
36             <property name="bankname"
37                       not-null="false"/>
38             <property name="swift"
39                       not-null="false"/>
40         </subclass>
41     </class>
42 
43 </hibernate-mapping>

 

三、优点

1.Remember that InheritanceType.SINGLE_TABLE enforces all columns of sub-classes to be nullable. One of the benefits of this mapping is that you can now declare columns of the CREDITCARD table as NOT NULL , guaranteeing data integrity.

 

四、缺点

代码中的第5点

posted @ 2016-04-08 11:52  shamgod  阅读(392)  评论(0编辑  收藏  举报
haha