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 ...
随机推荐
- 以Lockbits的方式访问bitmap
转载自:http://www.cnblogs.com/xiashengwang/p/4225848.html 2015-01-15 11:38 by xiashengwang, 585 阅读, 0 评 ...
- Linux下强制修改root密码方法(图)
如果Linux操作系统的root密码,那怎么办呢?方法很多,下面再给大家介绍一种. [1] 进入以下画面后,按下e按钮,进入编辑模式: [2]进入以下的画面后,选择如下所示的选项,再次按下e按钮: 然 ...
- HashCode作用
作用: 1.HashCode的存在主要是为了查找的快捷性,HashCode是用来在散列存储结构中确定镀锡的存储地址的 2.如果两个对象的equals相等,那么HashCode一定相等,反之不行 3.如 ...
- JS中的apply,call,bind深入理解
在Javascript中,Function是一种对象.Function对象中的this指向决定于函数被调用的方式.使用apply,call 与 bind 均可以改变函数对象中this的指向,在说区别之 ...
- Mac下安装Tomcat及配置
今天介绍Mac下Tomcat的安装及配置: 1.在搜索引擎(如:必应或百度)中搜索“Tomcat”,第一条搜索结果就是Tomcat官方地址: 2.在左侧选择“Tomcat8”或“Tomcat9”,我这 ...
- Visual Studio 2012 [ADO.NET 实体数据模型]丢失没有的解决方法
首先打开控制面板,看是否已经安装EF,如果已经安装,先卸载,然后,首先打开安装包,找到/packages/EFTools目录下的EFTools.msi,将它们复制自己计算机的某一目录下,例如:C:\t ...
- SL410K 在Ubuntu禁用触摸板
由于之前把系统自带的恢复去了,然后TouchPad一直不能禁用,而后我的410k就只装上ubuntu,想不到在ubuntu上,禁用/启用 触摸板这么方便. http://askubuntu.com/q ...
- 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- 【POJ】【2449】Remmarguts' Date
K短路/A* 经(luo)典(ti) K短路题目= = K短路学习:http://www.cnblogs.com/Hilda/p/3226692.html 流程: 先把所有边逆向,做一遍dijkstr ...
- linux系统非ROOT用户80端口不能启动tomcat问题的变通办法——通过Iptables端口转发
2010-07-17 13:21:42 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{ ...