JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)
一、结构
二、代码
1.
package org.jpwh.model.inheritance.mappedsuperclass; import javax.persistence.MappedSuperclass;
import javax.validation.constraints.NotNull; @MappedSuperclass
public abstract class BillingDetails { @NotNull
protected String owner; // ... protected BillingDetails() {
} protected BillingDetails(String owner) {
this.owner = owner;
} public String getOwner() {
return owner;
} public void setOwner(String owner) {
this.owner = owner;
}
}
2.
package org.jpwh.model.inheritance.mappedsuperclass; import org.jpwh.model.Constants; import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull; @Entity
@AttributeOverride(
name = "owner",
column = @Column(name = "CC_OWNER", nullable = false))
public class CreditCard extends BillingDetails { @Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id; @NotNull
protected String cardNumber; @NotNull
protected String expMonth; @NotNull
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 Long getId() {
return id;
} 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.mappedsuperclass; import org.jpwh.model.Constants; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull; @Entity
public class BankAccount extends BillingDetails { @Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id; @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 Long getId() {
return id;
} 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;
}
}
三、存在的问题
1.it doesn’t support polymorphic associations very well。You can’t have another entity mapped with a foreign key “referencing BILLINGDETAILS ”—there is no such table. This would be problematic in the domain model, because BillingDetails is associated with User ; both the CREDITCARD and BANKACCOUNT tables would need a foreign key reference to the USERS table. None of these issues can be easily resolved, so you should consider an alternative mapping strategy.
2.查父类时要查每个表。Hibernate must execute a query against the superclass as several SQL SELECT s, one for each concrete subclass. The JPA query select bd from BillingDetails bd requires two SQL statements:
select
ID, OWNER, ACCOUNT, BANKNAME, SWIFT
from
BANKACCOUNT
select
ID, CC_OWNER, CARDNUMBER, EXPMONTH, EXPYEAR
from
CREDITCARD
3.several different columns, of different tables, share exactly the same semantics.
结论:We recommend this approach (only) for the top level of your class hierarchy,where polymorphism isn’t usually required, and when modification of the superclass in the future is unlikely.
JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)的更多相关文章
- 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-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, s ...
- 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-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 ...
随机推荐
- P1699: [Usaco2007 Jan]Balanced Lineup排队
很明显是一道RMQ问题,倍增法,维护一下区域的最大/小值就行了. var n,i,j,q,f,t,times:longint; hmin,hmax:array[..,..] of longint; f ...
- [SSH服务]——一些安全性配置和补充实验
SSH 安全性和配置 转载于 http://www.ibm.com/developerworks/cn/aix/library/au-sshsecurity/ 对于一些之前列举的代码示例,许多系统管理 ...
- c++函数内部声明函数,在函数外面实现函数是可以的
这个具体有什么用我也不大清楚,只知道可以这样 #include <iostream> //#include "header1.h" using namespace st ...
- Bootstrap入门五:表格
table样式: .table:表格基本样式,很少的padding,灰色的细水平分隔线. .table-striped:斑马纹样式,隔行换色. .table-bordered:为表格和其中的每个单元格 ...
- Netsharp FAQ
1.启动服务器时候不能监听端口,出现如下错误: 回答: 这种问题应该是在win7环境下才有,是没有权限,要以管理员身份运行Netsharp.Elephant.Q.exe. 2.启动服务器的时候,服务端 ...
- ReactJS入门
React介绍 React是facebook开发基于组件驱动开发(CDD ) 的UI类库,相对于双向绑定的类库,如AngularJS,它采用单向数据流绑定.通过采用虚拟DOM的概念,是的他在性能和处理 ...
- 使用AzCopy跨账户迁移blob
昨天北美紧急通知要停掉几个开发和测试的订阅,当天必须完成,因为事情比较多,搞得有点我措手不及,但是唯一的遗憾是Azure VM. 因为在上面做了很多东西,很多资料和环境都是做好的,如果被删除掉实在可惜 ...
- SPOJ-SQRBR Square Brackets
原题传送:http://www.spoj.pl/problems/SQRBR 动态规划. 设f[i][j]表示前i个位置在合法情况下缺少j个右括号的方案数. 转移方程为: f[i][j] = f[i- ...
- NYOJ-86 找球号(一)AC 分类: NYOJ 2014-02-02 10:45 160人阅读 评论(0) 收藏
NO.1 单纯的傻傻的代码: #include<stdio.h> long long num[100000005]={0}; int main(){ int n, m, k; scanf( ...
- 项目分析(PLUG)
plug过程 .INIT_PLUG #define INIT_PLUG Plug::InitPlug g_InitPlug(true); //共享内存数据结构 struct PlugShareMemo ...