【hibernate】映射继承关系
转载:https://www.cnblogs.com/yangchongxing/p/10405151.html
===========================================
1、每个带有隐式多态的具体类使用一个表
2、每个带有联合的具体类使用一个表
3、每个类层次结构使用一个表
4、每个带有联结的子类使用一个表
===========================================
1、每个带有隐式多态的具体类使用一个表
@MappedSuperclass 超类的属性被忽略不会被持久化
package cn.ycx.study.hibernate.entity; import javax.persistence.MappedSuperclass; @MappedSuperclass public abstract class BillingDetails { protected String owner; public String getOwner() { return owner; } public void setOwner(String owner) { this.owner = owner; } }
子类
package cn.ycx.study.hibernate.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class CreditCard extends BillingDetails { @Id @GeneratedValue(generator="id_generator") protected Long id; protected String cardNumber; protected String expMonth; protected String expYear; public Long getId() { return id; } public void setId(Long id) { this.id = 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; } }
子类
package cn.ycx.study.hibernate.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class BankAccount extends BillingDetails { @Id @GeneratedValue(generator="id_generator") protected Long id; protected String account; protected String bankname; protected String swift; public Long getId() { return id; } public void setId(Long id) { this.id = 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; } }
生成的 SQL 表结构
CREATE TABLE `creditcard` ( `id` BIGINT(20) NOT NULL, `owner` VARCHAR(255) NULL DEFAULT NULL, `cardNumber` VARCHAR(255) NULL DEFAULT NULL, `expMonth` VARCHAR(255) NULL DEFAULT NULL, `expYear` VARCHAR(255) NULL DEFAULT NULL, PRIMARY KEY (`id`) ) COLLATE='utf8mb4_general_ci' ENGINE=InnoDB ; CREATE TABLE `bankaccount` ( `id` BIGINT(20) NOT NULL, `owner` VARCHAR(255) NULL DEFAULT NULL, `account` VARCHAR(255) NULL DEFAULT NULL, `bankname` VARCHAR(255) NULL DEFAULT NULL, `swift` VARCHAR(255) NULL DEFAULT NULL, PRIMARY KEY (`id`) ) COLLATE='utf8mb4_general_ci' ENGINE=InnoDB ;
2、每个带有联合的具体类使用一个表
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) 策略
package cn.ycx.study.hibernate.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; @Entity @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public abstract class BillingDetails { @Id @GeneratedValue(generator="id_generator") protected Long id; protected String owner; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getOwner() { return owner; } public void setOwner(String owner) { this.owner = owner; } }
子类
package cn.ycx.study.hibernate.entity; import javax.persistence.Entity; @Entity public class CreditCard extends BillingDetails { protected String cardNumber; protected String expMonth; protected String expYear; 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; } }
子类
package cn.ycx.study.hibernate.entity; import javax.persistence.Entity; @Entity public class BankAccount extends BillingDetails { protected String account; protected String bankname; protected String swift; 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; } }
生成的 SQL 表结构
CREATE TABLE `bankaccount` ( `id` BIGINT(20) NOT NULL, `owner` VARCHAR(255) NULL DEFAULT NULL, `account` VARCHAR(255) NULL DEFAULT NULL, `bankname` VARCHAR(255) NULL DEFAULT NULL, `swift` VARCHAR(255) NULL DEFAULT NULL, PRIMARY KEY (`id`) ) COLLATE='utf8mb4_general_ci' ENGINE=InnoDB ; CREATE TABLE `creditcard` ( `id` BIGINT(20) NOT NULL, `owner` VARCHAR(255) NULL DEFAULT NULL, `cardNumber` VARCHAR(255) NULL DEFAULT NULL, `expMonth` VARCHAR(255) NULL DEFAULT NULL, `expYear` VARCHAR(255) NULL DEFAULT NULL, PRIMARY KEY (`id`) ) COLLATE='utf8mb4_general_ci' ENGINE=InnoDB ;
3、每个类层次结构使用一个表
@Inheritance(strategy=InheritanceType.SINGLE_TABLE) 策略
@DiscriminatorColumn(name="bd_type") 指定识别器列名,不指定默认时 DTYPE。
@DiscriminatorValue("CC") 指定识别器值,每种一个,
package cn.ycx.study.hibernate.entity; import javax.persistence.DiscriminatorColumn; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="bd_type") public abstract class BillingDetails { @Id @GeneratedValue(generator="id_generator") protected Long id; protected String owner; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getOwner() { return owner; } public void setOwner(String owner) { this.owner = owner; } }
子类,识别器值 @DiscriminatorValue("CC")
package cn.ycx.study.hibernate.entity; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; @Entity @DiscriminatorValue("CC") public class CreditCard extends BillingDetails { protected String cardNumber; protected String expMonth; protected String expYear; 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; } }
子类,识别器值 @DiscriminatorValue("BA")
package cn.ycx.study.hibernate.entity; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; @Entity @DiscriminatorValue("BA") public class BankAccount extends BillingDetails { protected String account; protected String bankname; protected String swift; 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; } }
生成的 SQL 表结构
CREATE TABLE `billingdetails` ( `bd_type` VARCHAR(31) NOT NULL, `id` BIGINT(20) NOT NULL, `owner` VARCHAR(255) NULL DEFAULT NULL, `cardNumber` VARCHAR(255) NULL DEFAULT NULL, `expMonth` VARCHAR(255) NULL DEFAULT NULL, `expYear` VARCHAR(255) NULL DEFAULT NULL, `account` VARCHAR(255) NULL DEFAULT NULL, `bankname` VARCHAR(255) NULL DEFAULT NULL, `swift` VARCHAR(255) NULL DEFAULT NULL, PRIMARY KEY (`id`) ) COLLATE='utf8mb4_general_ci' ENGINE=InnoDB ;
4、每个带有联结的子类使用一个表
@Inheritance(strategy=InheritanceType.JOINED) 联结
@PrimaryKeyJoinColumn(name = "ba_id") 可以不指定,它会自动继承父类主键。
package cn.ycx.study.hibernate.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; @Entity @Inheritance(strategy=InheritanceType.JOINED) public abstract class BillingDetails { @Id @GeneratedValue(generator="id_generator") protected Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } protected String owner; public String getOwner() { return owner; } public void setOwner(String owner) { this.owner = owner; } }
子类
package cn.ycx.study.hibernate.entity; import javax.persistence.Entity; import javax.persistence.PrimaryKeyJoinColumn; @Entity @PrimaryKeyJoinColumn(name = "cc_id") public class CreditCard extends BillingDetails { protected String cardNumber; protected String expMonth; protected String expYear; 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; } }
子类
package cn.ycx.study.hibernate.entity; import javax.persistence.Entity; import javax.persistence.PrimaryKeyJoinColumn; @Entity @PrimaryKeyJoinColumn(name = "ba_id") public class BankAccount extends BillingDetails { protected String account; protected String bankname; protected String swift; 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; } }