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 ...
随机推荐
- .NET和Docker ,比翼双飞
DockerCon 2019本周将在旧金山举行 ,DockerCon 是从业者.贡献者.维护者.开发者和容器生态系统学习.网络和创新的一站式活动. .NET 团队博客发布了<一起使用.NET和D ...
- C/C++ 字符串与数字相互转换
一.利用stringstream类 1. 字符串到整数 stringstream sstr(str); int x; sstr >> x;(即从sstr中提取数据) ...
- uva1395 - Slim Span(最小生成树)
先判断是不是连通图,不是就输出-1. 否则,把边排序,从最小的边开始枚举最小生成树里的最短边,对每个最短边用Kruskal算法找出最大边. 或者也可以不先判断连通图,而是在枚举之后如果ans还是INF ...
- go语言redis使用(redigo)
通过一个例子来学习redigo的使用,其中主要使用到了redis的订阅与发布功能,以及redis连接池的实现 redis操作: // tcp连接redis rs, err := redis.Dial( ...
- ZOJ - 3201 Tree of Tree (树形背包)
题意:有一棵树,树上每个结点都有一个权值,求恰好包含k个结点的子树的最大权值. 设dp[i][j]为以结点i为根的树中包含j个结点的子树的最大权值,则可以把这个结点下的每棵子树中所包含的所有子树的大小 ...
- h5废弃的标签和属性及新增的标签和属性
一.废弃的标签和属性 1.表现性元素 a) basefont b) big c) center d) font e) strike f) tt 2.框架类元素 a) frame b) frameset ...
- 自动化框架httpClient实例
package com.auto.test.util; import java.net.SocketException;import java.net.SocketTimeoutException;i ...
- 页面报错Uncaught SyntaxError: Unexpected identifier
错误描述:未捕获的语法错误:意想不到的标识符. 如图所示:检查之后发现是页面js内缺少“,”引起的.添加之后就OK了.
- jsp有哪些内置对象?作用分别是什么?
JSP共有以下9种基本内置组件 1.request对象 客户端请求,此请求会包含来自GET/POST请求的参数通过它才能了解到客户的需求,然后做出响应. 2.response对象 响应客户请求的有关信 ...
- Linux 驱动编程知识
1.包含的头文件 1.1 GPIO相关操作 #include <asm/arch/gpio.h>