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 ...
随机推荐
- Python实现C4.5(信息增益率)
Python实现C4.5(信息增益率) 运行环境 Pyhton3 treePlotter模块(画图所需,不画图可不必) matplotlib(如果使用上面的模块必须) 计算过程 st=>star ...
- 加速传感器(CoreMotion) swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...
- mysql 5.7 64位 解压版安装
64位操作系统最好安装64位的mysql数据库,充分利用内存的寻址能力,对于windows而言,mysql官网只提供了32位的MSI安装程序,因为在windows下安装64位的mysql,选择解压版安 ...
- SQL SERVER定时任务执行跟踪--供远程查看 [原创]
一.背景 每次查需要优化的SQL都需要上外网,通过Profiler,报表或者DMV执行特定sql来查找,来回跑很麻烦,能不能在本地直接监控外网的好性能的SQL呢?方法是有的,我们可以通过把Profil ...
- Daily Scrum4
今天我们小组开会内容分为以下部分: part1:与负责这个项目的其他组进行会晤; part2:组内成员召开了简短会议,进行工作安排; part3:总结今日工作,对项目遇到的问题商讨解决办法; ◆Par ...
- JavaEDU614 团队第三周项目总结
JavaEDU614 团队第三周项目总结 本周,根据项目计划完成模块的设计代码 本项目主要是完成俄罗斯方块的基本操作.用户可以自己练习和娱乐.需要满足以下几点要求. (1)界面控制游戏开始.暂停和结束 ...
- 国内最快的jquery cdn
cdnjs.cn是cdnjs.com在国内的镜像服务,项目托管与著名的又拍云存储,目前又拍云在全国有几十个cdn节点,并且还在增加中. cdnjs.cn 托管的jquery相信会成为国内最快的jque ...
- sql中having的使用
where 和having有什么区别? where 是group by之前进行筛选,having是group by 之后进行统计的筛选,一般having会和group by一起使用,结合聚合函数,统计 ...
- html5之canvas练习
代码地址:github.com/peng666/blogs 在线地址:http://peng666.github.io/blogs/
- 如何用myeclispe远程调试tomcat
如何用myeclispe远程调试tomcat 在工作开发中,通常用本机进行代码编写,然后将编好的工程部署到测试服务器进行测试.往往测试服务器并不是自己的本机,因此对调试带来众多不便.今天学习可以用my ...