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.
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">)的更多相关文章
- 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 ...
- 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 ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)
一.结构 二.代码 1. package org.jpwh.model.inheritance.mappedsuperclass; import javax.persistence.MappedSup ...
- 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 ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-001Hibernate映射继承的方法
There are four different strategies for representing an inheritance hierarchy: Use one table per co ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-009Polymorphic collections(@OneToMany(mappedBy = "user")、@ManyToOne、)
一.代码 1. package org.jpwh.model.inheritance.associations.onetomany; import org.jpwh.model.Constants; ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-008Polymorphic many-to-one associations(@ManyToOne、@Inheritance、)
一.结构 二.代码 1. package org.jpwh.model.inheritance.associations.manytoone; import org.jpwh.model.Consta ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-007Inheritance of embeddable classes(@MappedSuperclass、@Embeddable、@AttributeOverrides、、)
一.结构 二.代码 1. package org.jpwh.model.inheritance.embeddable; import javax.persistence.MappedSuperclas ...
- 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 ...
随机推荐
- mac 上 mamp 配置虚拟主机 具体过程 ?
https://www.zhihu.com/question/32320396 mac 上 mamp 配置虚拟主机 具体过程 ? 按照哪些配置什么的 都搞好了 但是还不好 配置的主机 制定的目录 还是 ...
- I.MX6 dts 在哪里、怎么编译
/************************************************************************ * I.MX6 DTS 在哪里.怎么编译 * 说明: ...
- HttpServletRequest获取请求得URL信息
request对象中包含的是请求信息,当我们在浏览器地址栏上输入:http://localhost:8080/Example/AServlet?username=zhangsan,这段地址也会作为请求 ...
- 关于overflow:hidden
(本文只针对hidden这个值的用处进行阐述) 关于overflow:hidden;很多人都知道他是溢出隐藏的一个属性,但是并不是很多人知道它的一些神奇的地方!首先先讲一下众所周知的溢出隐藏吧! 溢出 ...
- 「长乐集训 2017 Day1」区间 线段树
题目 对于两个区间\((a,b),(c,d)\),若\(c < a < d\)或\(c < b < d\)则可以从\((a,b)\)走到\((c,d)\)去,现在有以下两种操作 ...
- CODEVS 1174 靶形数独
题目描述 Description 小城和小华都是热爱数学的好学生,最近,他们不约而同地 迷上了数独游戏,好胜的他们想用数独来一比高低.但普通 的数独对他们来说都过于简单了,于是他们向Z 博士请教,Z ...
- J2EE分布式服务基础之RPC
一.RPC介绍 什么是RPC 远程过程调用(RPC)是一个协议,程序可以使用这个协议请求网络中另一台计算机上某程序的服务而不需知道网络细节. RPC模型 C/S模式 基于传输层协议 (例如 TCP/I ...
- xj监控端口,模拟登陆脚本
#!/bin/bash date=`date +%Y%m%d-%H%M` count=0 ip1=124.117.246.195 ip2=124.117.246.194 port1=(443 80 6 ...
- Bug:DataGridCell的显示不完整
最近在使用DataGrid时遇到一个bug, 在客户机器上DataGrid的内容显示不完整, 具体表现为某些列的显示为空. 具体的可视树为:DataGridCell > ContentPrese ...
- UIButton 不同状态图片来回切换
做百度地图是否显示路况信息,用到两种状态的图片 第一种方法:用Bool进行标记 声明一个 Bool _isRoad; - (void)roadBtnAction:(UIButton *)sender ...