成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Hibernate繼承映射

開發(fā) 后端
在面向?qū)ο蟮某绦蝾I(lǐng)域中,類與類之間是有繼承關(guān)系的,例如Java世界中只需要extends關(guān)鍵字就可以確定這兩個(gè)類的父子關(guān)系,但是在關(guān)系數(shù)據(jù)庫(kù)的世界中,表與表之間沒有任何關(guān)鍵字可以明確指明這兩張表的父子關(guān)系,表與表是沒有繼承關(guān)系這樣的說法的。下文將詳細(xì)介紹Hibernate提供的3種方案。

在面向?qū)ο蟮某绦蝾I(lǐng)域中,類與類之間是有繼承關(guān)系的,例如Java世界中只需要extends關(guān)鍵字就可以確定這兩個(gè)類的父子關(guān)系,但是在關(guān)系數(shù)據(jù)庫(kù)的世界中,表與表之間沒有任何關(guān)鍵字可以明確指明這兩張表的父子關(guān)系,表與表是沒有繼承關(guān)系這樣的說法的。為了將程序領(lǐng)域中的繼承關(guān)系反映到數(shù)據(jù)中,Hibernate為我們提供了3種方案:

第一種方案:一個(gè)子類對(duì)應(yīng)一張表。
第二種方案:使用一張表表示所有繼承體系下的類的屬性的并集。
第三種方案:每個(gè)子類使用一張表只存儲(chǔ)它特有的屬性,然后與父類所對(duì)應(yīng)的表以一對(duì)一主鍵關(guān)聯(lián)的方式關(guān)聯(lián)起來。

現(xiàn)在假設(shè)有People、Student、Teacher三個(gè)類,父類為People,Student與Teacher為People的父類,代碼如下:

People類:

  1. public class People  
  2. {  
  3.     /*父類所擁有的屬性*/ 
  4.     private String id;  
  5.     private String name;  
  6.     private String sex;  
  7.     private String age;  
  8.     private Timestamp birthday;  
  9.       
  10.     /*get和set方法*/ 

Student類:

  1. public class Student extends People  
  2. {  
  3.     /*學(xué)生獨(dú)有的屬性*/ 
  4.     private String cardId;//學(xué)號(hào)  
  5.  
  6.     public String getCardId()  
  7.     {  
  8.         return cardId;  
  9.     }  
  10.  
  11.     public void setCardId(String cardId)  
  12.     {  
  13.         this.cardId = cardId;  
  14.     }  

Teacher類:

  1. public class Teacher extends People  
  2. {  
  3.     /*Teacher所獨(dú)有的屬性*/ 
  4.     private int salary;//工資  
  5.  
  6.     public int getSalary()  
  7.     {  
  8.         return salary;  
  9.     }  
  10.  
  11.     public void setSalary(int salary)  
  12.     {  
  13.         this.salary = salary;  
  14.     }  

第一種方案:一個(gè)子類對(duì)應(yīng)一張表 

該方案是使繼承體系中每一個(gè)子類都對(duì)應(yīng)數(shù)據(jù)庫(kù)中的一張表。示意圖如下:

每一個(gè)子類對(duì)應(yīng)的數(shù)據(jù)庫(kù)表都包含了父類的信息,并且包含了自己獨(dú)有的屬性。每個(gè)子類對(duì)應(yīng)一張表,而且這個(gè)表的信息是完備的,即包含了所有從父類繼承下來的屬性映射的字段。這種策略是使用<union-subclass>標(biāo)簽來定義子類的。

配置People.hbm.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
  3.  
  4. <hibernate-mapping> 
  5.     <class name="com.suxiaolei.hibernate.pojos.People" abstract="true"> 
  6.         <id name="id" type="string"> 
  7.             <column name="id"></column> 
  8.             <generator class="uuid"></generator> 
  9.         </id> 
  10.  
  11.         <property name="name" column="name" type="string"></property> 
  12.         <property name="sex" column="sex" type="string"></property> 
  13.         <property name="age" column="age" type="string"></property> 
  14.         <property name="birthday" column="birthday" type="timestamp"></property> 
  15.  
  16.         <!--   
  17.         <union-subclass name="com.suxiaolei.hibernate.pojos.Student" table="student">   
  18.             <property name="cardId" column="cardId" type="string"></property>   
  19.         </union-subclass>   
  20.         <union-subclass name="com.suxiaolei.hibernate.pojos.Teacher" table="teacher">   
  21.             <property name="salary" column="salary" type="integer"></property>   
  22.         </union-subclass>   
  23.         --> 
  24.     </class> 
  25.     <union-subclass name="com.suxiaolei.hibernate.pojos.Student" 
  26.         table="student" extends="com.suxiaolei.hibernate.pojos.People"> 
  27.         <property name="cardId" column="cardId" type="string"></property> 
  28.     </union-subclass> 
  29.  
  30.     <union-subclass name="com.suxiaolei.hibernate.pojos.Teacher" 
  31.         table="teacher" extends="com.suxiaolei.hibernate.pojos.People"> 
  32.         <property name="salary" column="salary" type="integer"></property> 
  33.     </union-subclass> 
  34. </hibernate-mapping> 

以上配置是一個(gè)子類一張表方案的配置,<union-subclass>標(biāo)簽是用于指示出該hbm文件所表示的類的子類,如People類有兩個(gè)子類,就需要兩個(gè)<union-subclass>標(biāo)簽以此類推。<union-subclass>標(biāo)簽的"name"屬性用于指定子類的全限定名稱,"table"屬性用于指定該子類對(duì)應(yīng)的表的名稱,"extends"屬性用于指定該子類的父類,注意該屬性與<union-subclass>標(biāo)簽的位置有關(guān),若 <union-subclass>標(biāo)簽作為<class>標(biāo)簽的子標(biāo)簽,則"extends"屬性可以不設(shè)置,否則需要明確設(shè)置"extends"屬性。<class>標(biāo)簽中的"abstract"屬性如果值為true則,不會(huì)生成表結(jié)構(gòu)。如果值為false則會(huì)生成表結(jié)構(gòu),但是不會(huì)插入數(shù)據(jù)。

根據(jù)People.hbm.xml生成表結(jié)構(gòu):

  1. drop table if exists student  
  2. drop table if exists teacher  
  3.  
  4.  create table student (  
  5.      id varchar(255) not null,  
  6.      name varchar(255),  
  7.      sex varchar(255),  
  8.      age varchar(255),  
  9.      birthday datetime,  
  10.      cardId varchar(255),  
  11.      primary key (id)  
  12.  )  
  13.  
  14.  create table teacher (  
  15.      id varchar(255) not null,  
  16.      name varchar(255),  
  17.      sex varchar(255),  
  18.      age varchar(255),  
  19.      birthday datetime,  
  20.      salary integer,  
  21.      primary key (id)  
  22.  ) 

可以看到一個(gè)子類對(duì)應(yīng)一張表。

第二種方案:使用一張表表示所有繼承體系下的類的屬性的并集

這種策略是使用<subclass>標(biāo)簽來實(shí)現(xiàn)的。因?yàn)轭惱^承體系下會(huì)有許多個(gè)子類,要把多個(gè)類的信息存放在一張表中,必須有某種機(jī)制來區(qū)分哪些記錄是屬于哪個(gè)類的。Hibernate中的這種機(jī)制就是,在表中添加一個(gè)字段,用這個(gè)字段的值來進(jìn)行區(qū)分。在表中添加這個(gè)標(biāo)示列使用<discriminator>標(biāo)簽來實(shí)現(xiàn)。

該策略的示意圖:

將繼承體系中的所有類信息表示在同一張表中后,只要是這個(gè)類沒有的屬性會(huì)被自動(dòng)賦上null。

配置People.hbm.xml:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
  3.  
  4. <hibernate-mapping> 
  5.     <class name="com.suxiaolei.hibernate.pojos.People" table="people"> 
  6.         <id name="id" type="string"> 
  7.             <column name="id"></column> 
  8.             <generator class="uuid"></generator> 
  9.         </id> 
  10.  
  11.         <discriminator column="peopleType" type="string"></discriminator> 
  12.  
  13.         <property name="name" column="name" type="string"></property> 
  14.         <property name="sex" column="sex" type="string"></property> 
  15.         <property name="age" column="age" type="string"></property> 
  16.         <property name="birthday" column="birthday" type="timestamp"></property> 
  17.  
  18.         <subclass name="com.suxiaolei.hibernate.pojos.Student" discriminator-value="student"> 
  19.             <property name="cardId" column="cardId" type="string"></property> 
  20.         </subclass> 
  21.           
  22.         <subclass name="com.suxiaolei.hibernate.pojos.Teacher" discriminator-value="teacher"> 
  23.             <property name="salary" column="salary" type="string"></property> 
  24.         </subclass> 
  25.     </class> 
  26. </hibernate-mapping> 

<discriminator>標(biāo)簽用于在表中創(chuàng)建一個(gè)標(biāo)識(shí)列,其"column"屬性指定標(biāo)識(shí)列的列名,"type"指定了標(biāo)識(shí)列的類型。<subclass>標(biāo)簽用于指定該HBM文件代表類的子類,有多少子類就有多少個(gè)該標(biāo)簽,其"name"屬性指定子類的名稱,"discriminator-value"屬性指定該子類的數(shù)據(jù)的標(biāo)識(shí)列的值是什么,其"extends"屬性與<union-subclass>的"extends"屬性用法一致。

根據(jù)People.hbm.xml生成表結(jié)構(gòu):

  1. drop table if exists people  
  2.  
  3. create table people (  
  4.     id varchar(255) not null,  
  5.     peopleType varchar(255) not null,  
  6.     name varchar(255),  
  7.     sex varchar(255),  
  8.     age varchar(255),  
  9.     birthday datetime,  
  10.     cardId varchar(255),  
  11.     salary varchar(255),  
  12.     primary key (id)  

可以看到一張表將繼承體系下的所有信息都包含了,其中"peopleType"為標(biāo)識(shí)列。

第三種方案:每個(gè)子類使用一張表只存儲(chǔ)它特有的屬性,然后與父類所對(duì)應(yīng)的表以一對(duì)一主鍵關(guān)聯(lián)的方式關(guān)聯(lián)起來。

這種策略是使用<joined-subclass>標(biāo)簽來定義子類的。父類、子類都對(duì)應(yīng)一張數(shù)據(jù)庫(kù)表。在父類對(duì)應(yīng)的數(shù)據(jù)庫(kù)表中,它存儲(chǔ)了所有記錄的公共信息,實(shí)際上該父類對(duì)應(yīng)的表會(huì)包含所有的記錄,包括父類和子類的記錄;在子類對(duì)應(yīng)的數(shù)據(jù)庫(kù)表中,這個(gè)表只定義了子類中所特有的屬性映射的字段。子類對(duì)應(yīng)的數(shù)據(jù)表與父類對(duì)應(yīng)的數(shù)據(jù)表,通過一對(duì)一主鍵關(guān)聯(lián)的方式關(guān)聯(lián)起來。

這種策略的示意圖:

people表中存儲(chǔ)了子類的所有記錄,但只記錄了他們共有的信息,而他們獨(dú)有的信息存儲(chǔ)在他們對(duì)應(yīng)的表中,一條記錄要獲得其獨(dú)有的信息,要通過people記錄的主鍵到其對(duì)應(yīng)的子表中查找主鍵值一樣的記錄然后取出它獨(dú)有的信息。

配置People.hbm.xml:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
  3.  
  4. <hibernate-mapping> 
  5.     <class name="com.suxiaolei.hibernate.pojos.People" table="people"> 
  6.         <id name="id" type="string"> 
  7.             <column name="id"></column> 
  8.             <generator class="uuid"></generator> 
  9.         </id> 
  10.  
  11.         <property name="name" column="name" type="string"></property> 
  12.         <property name="sex" column="sex" type="string"></property> 
  13.         <property name="age" column="age" type="string"></property> 
  14.         <property name="birthday" column="birthday" type="timestamp"></property> 
  15.           
  16.         <joined-subclass name="com.suxiaolei.hibernate.pojos.Student" table="student"> 
  17.             <key column="id"></key> 
  18.             <property name="cardId" column="cardId" type="string"></property> 
  19.         </joined-subclass> 
  20.           
  21.         <joined-subclass name="com.suxiaolei.hibernate.pojos.Teacher" table="teacher"> 
  22.             <key column="id"></key> 
  23.             <property name="salary" column="salary" type="integer"></property> 
  24.         </joined-subclass> 
  25.     </class> 
  26. </hibernate-mapping> 

<joined-subclass>標(biāo)簽需要包含一個(gè)key標(biāo)簽,這個(gè)標(biāo)簽指定了子類和父類之間是通過哪個(gè)字段來關(guān)聯(lián)的。

根據(jù)People.hbm.xml生成表結(jié)構(gòu):

  1. drop table if exists people  
  2. drop table if exists student  
  3. drop table if exists teacher  
  4.  
  5. create table people (  
  6.     id varchar(255) not null,  
  7.     name varchar(255),  
  8.     sex varchar(255),  
  9.     age varchar(255),  
  10.     birthday datetime,  
  11.     primary key (id)  
  12. )  
  13.  
  14. create table student (  
  15.     id varchar(255) not null,  
  16.     cardId varchar(255),  
  17.     primary key (id)  
  18. )  
  19.  
  20. create table teacher (  
  21.     id varchar(255) not null,  
  22.     salary integer,  
  23.     primary key (id)  
  24. )  
  25.  
  26. alter table student   
  27.     add index FK8FFE823BF9D436B1 (id),   
  28.     add constraint FK8FFE823BF9D436B1   
  29.     foreign key (id)   
  30.     references people (id)  
  31.  
  32. alter table teacher   
  33.     add index FKAA31CBE2F9D436B1 (id),   
  34.     add constraint FKAA31CBE2F9D436B1   
  35.     foreign key (id)   
  36.     references people (id) 

可以看到,父類對(duì)應(yīng)的表保存公有信息,子類對(duì)應(yīng)的表保存獨(dú)有信息,子類和父類對(duì)應(yīng)的表使用一對(duì)一主鍵關(guān)聯(lián)的方式關(guān)聯(lián)起來。

原文鏈接:http://www.cnblogs.com/otomedaybreak/archive/2012/01/26/2329809.html

【編輯推薦】

  1. Hibernate事務(wù)與并發(fā)問題處理
  2. 讓Hibernate顯示SQL語句的綁定參數(shù)值
  3. Hibernate延遲加載剖析與代理模式應(yīng)用
  4. 選用Ibatis和Hibernate的區(qū)別
  5. Hibernate攔截器與監(jiān)聽器

 

責(zé)任編輯:林師授 來源: 音①曉的博客
相關(guān)推薦

2009-09-25 14:20:28

Hibernate繼承映射

2009-06-16 14:36:54

Hibernate繼承

2012-05-30 15:03:43

ibmdw

2009-09-25 14:12:16

Hibernate繼承

2009-07-02 09:40:14

Hibernate的繼

2009-09-22 15:10:22

Hibernate映射

2009-09-23 17:34:18

Hibernate映射

2009-09-25 10:00:47

Hibernate映射

2012-02-03 11:17:33

HibernateJava

2012-02-03 10:07:04

HibernateJava

2009-06-02 14:46:26

Hibernate關(guān)系映射教程

2009-09-23 13:26:10

Hibernate對(duì)象

2009-09-25 12:31:13

Hibernate映射

2009-09-25 09:46:02

Hibernate高級(jí)

2009-09-25 12:59:52

Hibernate映射

2009-09-27 10:02:29

定制Hibernate

2009-09-28 14:54:33

Hibernate映射

2009-09-29 15:58:22

Hibernate映射

2009-06-18 14:22:06

Hibernate多對(duì)Hibernate

2012-03-19 16:27:05

JavaHibernate
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 欧美久久久电影 | 日本国产欧美 | 中文字幕乱码一区二区三区 | 成人片在线看 | 中文字幕国产日韩 | 中文字幕在线一 | 国产亚洲一区精品 | 好姑娘影视在线观看高清 | 一区二区三区国产精品 | 国产乱码精品一区二区三区中文 | 在线91 | 亚洲一区二区三区高清 | 国产精品久久久久免费 | 国产黄色大片网站 | 欧美天堂 | 国产高清一区二区 | 亚洲一区精品视频 | 6080亚洲精品一区二区 | 91看片免费版 | 99精品视频在线 | 国产一区二区三区在线看 | 黄片毛片| 免费观看一级特黄欧美大片 | 亚洲欧洲成人av每日更新 | 这里只有精品99re | 免费在线h视频 | 国产在线视频在线观看 | 久久精品91久久久久久再现 | 99视频精品 | 香蕉一区二区 | 午夜影晥 | 麻豆av片 | 美女中文字幕视频 | 国产在线观看一区二区三区 | 精品欧美一区二区三区免费观看 | 91亚洲精品在线 | 91视频91| 午夜欧美 | 精品综合 | 在线观看视频你懂得 | 日韩成人中文字幕 |