一、结构

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.

 package org.jpwh.model.inheritance.mixed;

 import org.jpwh.model.Constants;

 import javax.persistence.DiscriminatorColumn;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.validation.constraints.NotNull; @Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "BD_TYPE")
public abstract class BillingDetails { @Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id; @NotNull
protected String owner; protected BillingDetails() {
} protected BillingDetails(String owner) {
this.owner = owner;
} public Long getId() {
return id;
} public String getOwner() {
return owner;
} public void setOwner(String owner) {
this.owner = owner;
}
}

2.

 package org.jpwh.model.inheritance.mixed;

 import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.validation.constraints.NotNull; @Entity
@DiscriminatorValue("CC")
@SecondaryTable(
name = "CREDITCARD",
pkJoinColumns = @PrimaryKeyJoinColumn(name = "CREDITCARD_ID")
)
public class CreditCard extends BillingDetails { @NotNull // Ignored by JPA for DDL, strategy is SINGLE_TABLE!
@Column(table = "CREDITCARD", nullable = false) // Override the primary table
protected String cardNumber; @Column(table = "CREDITCARD", nullable = false)
protected String expMonth; @Column(table = "CREDITCARD", nullable = false)
protected String expYear; // ...
public CreditCard() {
super();
} public CreditCard(String owner, String cardNumber, String expMonth, String expYear) {
super(owner);
this.cardNumber = cardNumber;
this.expMonth = expMonth;
this.expYear = expYear;
} public String getCardNumber() {
return cardNumber;
} public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
} public String getExpMonth() {
return expMonth;
} public void setExpMonth(String expMonth) {
this.expMonth = expMonth;
} public String getExpYear() {
return expYear;
} public void setExpYear(String expYear) {
this.expYear = expYear;
}
}

3.

 package org.jpwh.model.inheritance.mixed;

 import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.validation.constraints.NotNull; @Entity
@DiscriminatorValue("BA")
public class BankAccount extends BillingDetails { @NotNull
protected String account; @NotNull
protected String bankname; @NotNull
protected String swift; public BankAccount() {
super();
} public BankAccount(String owner, String account, String bankname, String swift) {
super(owner);
this.account = account;
this.bankname = bankname;
this.swift = swift;
} public String getAccount() {
return account;
} public void setAccount(String account) {
this.account = account;
} public String getBankname() {
return bankname;
} public void setBankname(String bankname) {
this.bankname = bankname;
} public String getSwift() {
return swift;
} public void setSwift(String swift) {
this.swift = swift;
}
}

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

 select
ID, OWNER, ACCOUNT, BANKNAME, SWIFT,
EXPMONTH, EXPYEAR, CARDNUMBER,
BD_TYPE
from
BILLINGDETAILS
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

 <?xml version="1.0"?>
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/orm/hbm"
package="org.jpwh.model.inheritance.mixed"
default-access="field"> <class name="BillingDetails"
abstract="true">
<id name="id">
<generator class="native"/>
</id>
<discriminator column="BD_TYPE" type="string"/>
<property name="owner"
not-null="true"/> <subclass name="CreditCard"
discriminator-value="CC">
<join table="CREDITCARD" fetch="select">
<key column="CREDITCARD_ID"/>
<property name="cardNumber"
column="CARDNUMBER"
not-null="true"/> <property name="expMonth"
column="EXPMONTH"
not-null="true"/>
<property name="expYear"
column="EXPYEAR"
not-null="true"/>
</join>
</subclass> <subclass name="BankAccount"
discriminator-value="BA">
<property name="account"
not-null="false"/>
<property name="bankname"
not-null="false"/>
<property name="swift"
not-null="false"/>
</subclass>
</class> </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点

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

  1. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-005Table per subclass with joins(@Inheritance(strategy = InheritanceType.JOINED)、@PrimaryKeyJoinColumn、)

    一.结构 The fourth option is to represent inheritance relationships as SQL foreign key associations. Ev ...

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

    一.代码 1. package org.jpwh.model.inheritance.tableperclass; import org.jpwh.model.Constants; import ja ...

  3. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.mappedsuperclass; import javax.persistence.MappedSup ...

  4. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-004Table per class hierarchy(@Inheritance..SINGLE_TABLE)、@DiscriminatorColumn、@DiscriminatorValue、@DiscriminatorFormula)

    一.结构 You can map an entire class hierarchy to a single table. This table includes columns for all pr ...

  5. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-001Hibernate映射继承的方法

    There are four different strategies for representing an inheritance hierarchy: Use one table per co ...

  6. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-009Polymorphic collections(@OneToMany(mappedBy = "user")、@ManyToOne、)

    一.代码 1. package org.jpwh.model.inheritance.associations.onetomany; import org.jpwh.model.Constants; ...

  7. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-008Polymorphic many-to-one associations(@ManyToOne、@Inheritance、)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.associations.manytoone; import org.jpwh.model.Consta ...

  8. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-007Inheritance of embeddable classes(@MappedSuperclass、@Embeddable、@AttributeOverrides、、)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.embeddable; import javax.persistence.MappedSuperclas ...

  9. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-007UserTypes的用法(@org.hibernate.annotations.Type、@org.hibernate.annotations.TypeDefs、CompositeUserType、DynamicParameterizedType、、、)

    一.结构 二.Hibernate支持的UserTypes接口  UserType —You can transform values by interacting with the plain JD ...

随机推荐

  1. canvas基础学习(二)

    一.图像绘制 canvas绘制图像的方法是ctx.drawImage();该方法有三种使用方式 1.ctx.drawImage(img,x,y); img是指图像对象,x和y分别是这个图像左上角在ca ...

  2. hdu1863(最小生成树)

    很裸的最小生成树,但要注意判断输出问号的情况.其实就是当给的图不是连通图时输出问号.判断方法是:看形成的最小生成树的边数是不是等于节点数减一. #include<iostream> #in ...

  3. php-fpm 和 mysql 之间的关系

    我们都知道,php是不能直接操作 mysql的,他需要通过扩展提供接口调用,php的mysql扩展也好几个,只支持面向过程的mysql,既支持面向过程也支持面向对象的mysqli,只支持面向对象的PD ...

  4. mysql触发器与hash索引

    url查询哈希值的维护 触发器 2.1 创建表 pseudohash. 2.2 创建触发器,当对表进行插入和更新时,触发 触发器 delimiter |create trigger pseudohas ...

  5. redhat5.8 alt+ctrl+f1 黑屏

    /********************************************************************** * redhat5.8 alt+ctrl+f1 黑屏 * ...

  6. UVA 10417 Gift Exchanging

    #include <iostream> #include <cstring> #include <stdio.h> #include <math.h> ...

  7. 【java规则引擎】之Drools引擎中模拟ReteooStatefulSession内部设计结构

    该片文章只是抽取drools中java代码实现的一些代码结构,帮助我们理解drools是如何实现rete算法的. 该部分只是抽取ReteooStatefulSession工作过程中的代码架构 利用了多 ...

  8. 学习动态性能表(20)--v$waitstat

    学习动态性能表 第20篇--V$WAITSTAT  2007.6.15 本视图保持自实例启动所有的等待事件统计信息.常用于当你发现系统存在大量的"buffer busy waits" ...

  9. 通过Azure File Service搭建基于iscsi的共享盘

    在Azure上目前已经有基于Samba协议的共享存储了. 但目前在Azure上,还不能把Disk作为共享盘.而在实际的应用部署中,共享盘是做集群的重要组件之一.比如仲裁盘.Shared Disk等. ...

  10. Update多个字段从一个表中

    UPDATE XXXXXX S SET (S.XXX, S.CCC, S.DDD, S.AAA, S.BBB) = (SELECT F.XXX, F.CCC, F.AAA, BBB FROM XXXX ...