Hibernate中的Entity类之间的继承关系之一MappedSuperclass

来源:互联网 发布:租房网站推荐 知乎 编辑:程序博客网 时间:2024/06/07 19:25

在hibernate中,Entity类可以继承Entity类或非Entity类。但是,关系数据库表之间不存在继承的关系。那么在Entity类之间的继承关系,在数据库表中如何表示呢?

Hibernate提供了4种兼容JPA的策略,解决Entity类的继承与关系数据库表的对应不匹配问题。这里介绍第一种MappedSuperclass。

在这种策略中,存在如下特征:

  • 只在Entity类之间存在继承关系,其中的父Entity类使用@javax.persistence.MappedSuperclass标注。
  • 在关系数据库中没有父Entity类,一个具体子Entity类对应一个表,其中包含一个具体子Entity类的全部属性(包含父Entity类的属性)。

示例中,父Entity类定义如下:

[java] view plain copy
  1. @MappedSuperclass  
  2. public static class Account {  
  3.   
  4.     @Id  
  5.     private Long id;  
  6.   
  7.     private String owner;  
  8.   
  9.     private BigDecimal balance;  
  10.   
  11.     private BigDecimal interestRate;  
  12.   
  13.     public Long getId() {  
  14.         return id;  
  15.     }  
  16.   
  17.     public void setId(Long id) {  
  18.         this.id = id;  
  19.     }  
  20.   
  21.     public String getOwner() {  
  22.         return owner;  
  23.     }  
  24.   
  25.     public void setOwner(String owner) {  
  26.         this.owner = owner;  
  27.     }  
  28.   
  29.     public BigDecimal getBalance() {  
  30.         return balance;  
  31.     }  
  32.   
  33.     public void setBalance(BigDecimal balance) {  
  34.         this.balance = balance;  
  35.     }  
  36.   
  37.     public BigDecimal getInterestRate() {  
  38.         return interestRate;  
  39.     }  
  40.   
  41.     public void setInterestRate(BigDecimal interestRate) {  
  42.         this.interestRate = interestRate;  
  43.     }  
  44. }  

子Entity类定义如下:

[java] view plain copy
  1. @Entity(name = "DebitAccount")  
  2. public static class DebitAccount extends Account {  
  3.   
  4.     private BigDecimal overdraftFee;  
  5.   
  6.     public BigDecimal getOverdraftFee() {  
  7.         return overdraftFee;  
  8.     }  
  9.   
  10.     public void setOverdraftFee(BigDecimal overdraftFee) {  
  11.         this.overdraftFee = overdraftFee;  
  12.     }  
  13. }  


另一个子Entity类定义如下:

[java] view plain copy
  1. @Entity(name = "CreditAccount")  
  2. public static class CreditAccount extends Account {  
  3.   
  4.     private BigDecimal creditLimit;  
  5.   
  6.     public BigDecimal getCreditLimit() {  
  7.         return creditLimit;  
  8.     }  
  9.   
  10.     public void setCreditLimit(BigDecimal creditLimit) {  
  11.         this.creditLimit = creditLimit;  
  12.     }  
  13. }  


数据库表结构如下:

[sql] view plain copy
  1. CREATE TABLE DebitAccount (  
  2.     id BIGINT NOT NULL ,  
  3.     balance NUMERIC(19, 2) ,  
  4.     interestRate NUMERIC(19, 2) ,  
  5.     owner VARCHAR(255) ,  
  6.     overdraftFee NUMERIC(19, 2) ,  
  7.     PRIMARY KEY ( id )  
  8. )  
  9.   
  10. CREATE TABLE CreditAccount (  
  11.     id BIGINT NOT NULL ,  
  12.     balance NUMERIC(19, 2) ,  
  13.     interestRate NUMERIC(19, 2) ,  
  14.     owner VARCHAR(255) ,  
  15.     creditLimit NUMERIC(19, 2) ,  
  16.     PRIMARY KEY ( id )  
  17. )  
阅读全文
0 0
原创粉丝点击