Hibernate組件映射

來源:互聯網
上載者:User

標籤:

組件Components

除了粗粒度的物件模型設計(一個表映射成一個持久化類)之外,還可以採用細粒度的物件模型,吧一個表的映射成兩個或者多個類。

被細化出來的類,可以稱為組件(Component)

組件映射

組件映射的單向關聯

組件映射的雙向關聯

組件集合映射 

使用組件集合,可以讓組件對象的集合依附於一個持久化對象上

 

本來說自己寫的,網上看了看資料太全面了  就收集整理好了

 

Component這個概念在Hibernate中幾處不同的地方為了不同的目的被重複使用.

依賴對象(Dependent objects)

Component是一個被包含的對象,它作為實值型別被持久化,而非一個實體。“component(組件)”這一術語指的是物件導向的合成概念(而並不是系統構架層次上的組件的概念)舉個例子, 你可以對人(Person)如以下這樣來建模:

Java代碼  
  1. public class Person {  
  2.     private java.util.Date birthday;  
  3.     private Name name;  
  4.     private String key;  
  5.     public String getKey() {  
  6.         return key;  
  7.     }  
  8.     private void setKey(String key) {  
  9.         this.key=key;  
  10.     }  
  11.     public java.util.Date getBirthday() {  
  12.         return birthday;  
  13.     }  
  14.     public void setBirthday(java.util.Date birthday) {  
  15.         this.birthday = birthday;  
  16.     }  
  17.     public Name getName() {  
  18.         return name;  
  19.     }  
  20.     public void setName(Name name) {  
  21.         this.name = name;  
  22.     }  
  23.     ......  
  24.     ......  
  25. }  

 

Java代碼  
  1. public class Name {  
  2.     char initial;  
  3.     String first;  
  4.     String last;  
  5.     public String getFirst() {  
  6.         return first;  
  7.     }  
  8.     void setFirst(String first) {  
  9.         this.first = first;  
  10.     }  
  11.     public String getLast() {  
  12.         return last;  
  13.     }  
  14.     void setLast(String last) {  
  15.         this.last = last;  
  16.     }  
  17.     public char getInitial() {  
  18.         return initial;  
  19.     }  
  20.     void setInitial(char initial) {  
  21.         this.initial = initial;  
  22.     }  
  23. }  

 

現在,姓名(Name)是作為人(Person)的一個組成部分。需要注意的是:需要對姓名 的持久化屬性定義getter和setter方法,但是不需要實現任何的介面或申明標識符欄位。

以下是這個例子的Hibernate對應檔:

Xml代碼  
  1. <class name="eg.Person" table="person">  
  2.     <id name="Key" column="pid" type="string">  
  3.         <generator class="uuid.hex"/>  
  4.     </id>  
  5.     <property name="birthday" type="date"/>  
  6.     <component name="Name" class="eg.Name"> <!-- class attribute optional -->  
  7.         <property name="initial"/>  
  8.         <property name="first"/>  
  9.         <property name="last"/>  
  10.     </component>  
  11. </class>  

 

人員(Person)表中將包括pid, birthday, initial, first和 last等欄位。

就像所有的實值型別一樣, Component不支援共用引用。Component的值為空白從語意學上來講是專有的。 每當 重新載入一個包含組件的對象,如果component的所有欄位為空白,那麼將Hibernate將假定整個component為 空。對於絕大多數目的,這樣假定是沒有問題的。

Component的屬性可以是Hibernate類型(包括Collections, many-to-one 關聯, 以及其它Component 等等)。嵌套Component不應該作為特殊的應用被考慮(Nested components should not be considered an exotic usage)。 Hibernate趨向於支援設計細緻(fine-grained)的物件模型。

<component> 元素還允許有 <parent>子項目 ,用來表明component類中的一個屬性返回包含它的實體的引用。

Xml代碼  
  1. <class name="eg.Person" table="person">  
  2.     <id name="Key" column="pid" type="string">  
  3.         <generator class="uuid.hex"/>  
  4.     </id>  
  5.     <property name="birthday" type="date"/>  
  6.     <component name="Name" class="eg.Name">  
  7.         <parent name="namedPerson"/> <!-- reference back to the Person -->  
  8.         <property name="initial"/>  
  9.         <property name="first"/>  
  10.         <property name="last"/>  
  11.     </component>  
  12. </class>  

 

Hibernate組件映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.