一、结构

二、代码

1.

  1. package org.jpwh.model.inheritance.mappedsuperclass;
  2.  
  3. import javax.persistence.MappedSuperclass;
  4. import javax.validation.constraints.NotNull;
  5.  
  6. @MappedSuperclass
  7. public abstract class BillingDetails {
  8.  
  9. @NotNull
  10. protected String owner;
  11.  
  12. // ...
  13.  
  14. protected BillingDetails() {
  15. }
  16.  
  17. protected BillingDetails(String owner) {
  18. this.owner = owner;
  19. }
  20.  
  21. public String getOwner() {
  22. return owner;
  23. }
  24.  
  25. public void setOwner(String owner) {
  26. this.owner = owner;
  27. }
  28. }

2.

  1. package org.jpwh.model.inheritance.mappedsuperclass;
  2.  
  3. import org.jpwh.model.Constants;
  4.  
  5. import javax.persistence.AttributeOverride;
  6. import javax.persistence.Column;
  7. import javax.persistence.Entity;
  8. import javax.persistence.GeneratedValue;
  9. import javax.persistence.Id;
  10. import javax.validation.constraints.NotNull;
  11.  
  12. @Entity
  13. @AttributeOverride(
  14. name = "owner",
  15. column = @Column(name = "CC_OWNER", nullable = false))
  16. public class CreditCard extends BillingDetails {
  17.  
  18. @Id
  19. @GeneratedValue(generator = Constants.ID_GENERATOR)
  20. protected Long id;
  21.  
  22. @NotNull
  23. protected String cardNumber;
  24.  
  25. @NotNull
  26. protected String expMonth;
  27.  
  28. @NotNull
  29. protected String expYear;
  30.  
  31. // ...
  32.  
  33. public CreditCard() {
  34. super();
  35. }
  36.  
  37. public CreditCard(String owner, String cardNumber, String expMonth, String expYear) {
  38. super(owner);
  39. this.cardNumber = cardNumber;
  40. this.expMonth = expMonth;
  41. this.expYear = expYear;
  42. }
  43.  
  44. public Long getId() {
  45. return id;
  46. }
  47.  
  48. public String getCardNumber() {
  49. return cardNumber;
  50. }
  51.  
  52. public void setCardNumber(String cardNumber) {
  53. this.cardNumber = cardNumber;
  54. }
  55.  
  56. public String getExpMonth() {
  57. return expMonth;
  58. }
  59.  
  60. public void setExpMonth(String expMonth) {
  61. this.expMonth = expMonth;
  62. }
  63.  
  64. public String getExpYear() {
  65. return expYear;
  66. }
  67.  
  68. public void setExpYear(String expYear) {
  69. this.expYear = expYear;
  70. }
  71.  
  72. }

3.

  1. package org.jpwh.model.inheritance.mappedsuperclass;
  2.  
  3. import org.jpwh.model.Constants;
  4.  
  5. import javax.persistence.Entity;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.Id;
  8. import javax.validation.constraints.NotNull;
  9.  
  10. @Entity
  11. public class BankAccount extends BillingDetails {
  12.  
  13. @Id
  14. @GeneratedValue(generator = Constants.ID_GENERATOR)
  15. protected Long id;
  16.  
  17. @NotNull
  18. protected String account;
  19.  
  20. @NotNull
  21. protected String bankname;
  22.  
  23. @NotNull
  24. protected String swift;
  25.  
  26. public BankAccount() {
  27. super();
  28. }
  29.  
  30. public BankAccount(String owner, String account, String bankname, String swift) {
  31. super(owner);
  32. this.account = account;
  33. this.bankname = bankname;
  34. this.swift = swift;
  35. }
  36.  
  37. public Long getId() {
  38. return id;
  39. }
  40.  
  41. public String getAccount() {
  42. return account;
  43. }
  44.  
  45. public void setAccount(String account) {
  46. this.account = account;
  47. }
  48.  
  49. public String getBankname() {
  50. return bankname;
  51. }
  52.  
  53. public void setBankname(String bankname) {
  54. this.bankname = bankname;
  55. }
  56.  
  57. public String getSwift() {
  58. return swift;
  59. }
  60.  
  61. public void setSwift(String swift) {
  62. this.swift = swift;
  63. }
  64. }

三、存在的问题

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:

  1. select
  2. ID, OWNER, ACCOUNT, BANKNAME, SWIFT
  3. from
  4. BANKACCOUNT
  5. select
  6. ID, CC_OWNER, CARDNUMBER, EXPMONTH, EXPYEAR
  7. from
  8. 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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-001Hibernate映射继承的方法

    There are four different strategies for representing an inheritance hierarchy: Use one table per co ...

  6. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-009Polymorphic collections(@OneToMany(mappedBy = "user")、@ManyToOne、)

    一.代码 1. package org.jpwh.model.inheritance.associations.onetomany; import org.jpwh.model.Constants; ...

  7. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-008Polymorphic many-to-one associations(@ManyToOne、@Inheritance、)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.associations.manytoone; import org.jpwh.model.Consta ...

  8. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-007Inheritance of embeddable classes(@MappedSuperclass、@Embeddable、@AttributeOverrides、、)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.embeddable; import javax.persistence.MappedSuperclas ...

  9. 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 ...

随机推荐

  1. 以Lockbits的方式访问bitmap

    转载自:http://www.cnblogs.com/xiashengwang/p/4225848.html 2015-01-15 11:38 by xiashengwang, 585 阅读, 0 评 ...

  2. Linux下强制修改root密码方法(图)

    如果Linux操作系统的root密码,那怎么办呢?方法很多,下面再给大家介绍一种. [1] 进入以下画面后,按下e按钮,进入编辑模式: [2]进入以下的画面后,选择如下所示的选项,再次按下e按钮: 然 ...

  3. HashCode作用

    作用: 1.HashCode的存在主要是为了查找的快捷性,HashCode是用来在散列存储结构中确定镀锡的存储地址的 2.如果两个对象的equals相等,那么HashCode一定相等,反之不行 3.如 ...

  4. JS中的apply,call,bind深入理解

    在Javascript中,Function是一种对象.Function对象中的this指向决定于函数被调用的方式.使用apply,call 与 bind 均可以改变函数对象中this的指向,在说区别之 ...

  5. Mac下安装Tomcat及配置

    今天介绍Mac下Tomcat的安装及配置: 1.在搜索引擎(如:必应或百度)中搜索“Tomcat”,第一条搜索结果就是Tomcat官方地址: 2.在左侧选择“Tomcat8”或“Tomcat9”,我这 ...

  6. Visual Studio 2012 [ADO.NET 实体数据模型]丢失没有的解决方法

    首先打开控制面板,看是否已经安装EF,如果已经安装,先卸载,然后,首先打开安装包,找到/packages/EFTools目录下的EFTools.msi,将它们复制自己计算机的某一目录下,例如:C:\t ...

  7. SL410K 在Ubuntu禁用触摸板

    由于之前把系统自带的恢复去了,然后TouchPad一直不能禁用,而后我的410k就只装上ubuntu,想不到在ubuntu上,禁用/启用 触摸板这么方便. http://askubuntu.com/q ...

  8. 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  9. 【POJ】【2449】Remmarguts' Date

    K短路/A* 经(luo)典(ti) K短路题目= = K短路学习:http://www.cnblogs.com/Hilda/p/3226692.html 流程: 先把所有边逆向,做一遍dijkstr ...

  10. linux系统非ROOT用户80端口不能启动tomcat问题的变通办法——通过Iptables端口转发

    2010-07-17 13:21:42 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{ ...