`

Hibernate中映射集合属性

 
阅读更多

集合属性大致有两种:第一种是单纯的集合属性,如像 List、Set 或数组等集合属性;另一种是Map结构的集合属性,每个属性值都有对应的Key映射。

  集合映射的元素大致有如下几种:

  • List: 用于映射 List 集合属性
  • Set: 用于映射 Set 集合属性
  • Map: 用于映射 Map 集合性
  • Array: 用于映射数组集合属性
  • Bag: 用于映射无序集合
  • idbag: 用于映射无序集合,但为集合增加逻辑次序

 

1. List 集合属性
List是有序集合,因此持久化到数据库时也必须增加一列来表示集合元素的次序。看下面的持久化类,该 News 类有个集合属性:schools ,该属性对应学校。而集合属性只能以接口声明,因此下面代码中,schools 的类型能是List ,不能是ArrayList ,但该集合属性必须使用实现类完成初始化。

 

 

Java代码 复制代码 收藏代码
  1. package cn.janwer;     
  2.     
  3. import java.io.Serializable;     
  4. import java.util.ArrayList;     
  5. import java.util.List;     
  6.      
  7. public class News implements Serializable {     
  8.     int id;     
  9.     String title;     
  10.     String content;     
  11.     private List schools =  new  ArrayList();     
  12.          
  13.     public String getContent() {     
  14.           return  content;     
  15.     }     
  16.       
  17.     public void  setContent(String content) {     
  18.           this .content = content;     
  19.      }     
  20.         
  21.     public int  getId() {     
  22.          return  id;     
  23.      }     
  24.        
  25.     public void  setId( int  id) {     
  26.          this .id = id;     
  27.      }     
  28.         
  29.     public String getTitle() {     
  30.          return  title;     
  31.      }     
  32.         
  33.     public void  setTitle(String title) {     
  34.          this .title = title;     
  35.     }     
  36.          
  37.     public List getSchools() {     
  38.          return  schools;     
  39.     }     
  40.       
  41.      public void  setSchools(List schools) {     
  42.           this .schools = schools;     
  43.      }     
  44.  }   
package cn.janwer;   
  
import java.io.Serializable;   
import java.util.ArrayList;   
import java.util.List;   
   
public class News implements Serializable {   
    int id;   
    String title;   
    String content;   
    private List schools =  new  ArrayList();   
       
    public String getContent() {   
          return  content;   
    }   
    
    public void  setContent(String content) {   
          this .content = content;   
     }   
      
    public int  getId() {   
         return  id;   
     }   
     
    public void  setId( int  id) {   
         this .id = id;   
     }   
      
    public String getTitle() {   
         return  title;   
     }   
      
    public void  setTitle(String title) {   
         this .title = title;   
    }   
       
    public List getSchools() {   
         return  schools;   
    }   
    
     public void  setSchools(List schools) {   
          this .schools = schools;   
     }   
 } 

 

在作相应映射时,list元素要求用list-index的子元素来映射有序集合的次序列。集合的属性的值会存放有另外的表中,不可能与持久化类存储在同一个表内。因此须以外键关联,用Key元素来映射该外键列。

下面是该持久化类的映射文件:

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="cn.janwer.News" table="news">     
  6.    <id name="id" column="pid">     
  7.     <generator class="identity"/>     
  8.    </id>     
  9.         
  10.     <property name="title" length="50" column="TITLE"/>     
  11.     <property name="content" length="50" column="CONTENT"/>     
  12.     <list name="schools" table="school">  
  13.       <key column="pid" not-null="true"/>  
  14.       <list-index column="list_order" />  
  15.       <element type="string" column="school_name"/>     
  16.     </list>     
  17.       
  18. </class>     
  19. </hibernate-mapping>  
<xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
   
<hibernate-mapping >   
 <class name="cn.janwer.News" table="news">   
   <id name="id" column="pid">   
    <generator class="identity"/>   
   </id>   
      
    <property name="title" length="50" column="TITLE"/>   
    <property name="content" length="50" column="CONTENT"/>   
    <list name="schools" table="school">
      <key column="pid" not-null="true"/>
      <list-index column="list_order" />
      <element type="string" column="school_name"/>   
    </list>   
    
</class>   
</hibernate-mapping>

 

2. Set 集合属性
Set 集合属性映射与 List 非常相似,但因为 Set 是无序的,不可重复的集合。因此 set 元素无须使用 index 元素来指定集合元素次序。
映射文件与 List 相似,区别在于使用 set 元素时,无须增加 index 列来保存集合的次序。如下映射文件:

Xml代码 复制代码 收藏代码
  1. <set name="schools" table="school">  
  2.    <key column="pid" not-null="true"/>     
  3.    <element type="string" column="school_name" not-null="true"/>     
  4. </set>   
<set name="schools" table="school">
   <key column="pid" not-null="true"/>   
   <element type="string" column="school_name" not-null="true"/>   
</set> 

 

3. bag 元素映射
bag 元素既可以为 List 集合属性映射,也可以为 Collection 集合属性映射。不管是哪种集合属性,使用 bag 元素都将被映射成无序集合,而集合属性对应的表没有

Xml代码 复制代码 收藏代码
  1. <bag name="school" table="schools" >         
  2.   <key column="pid" not-null="true" />         
  3.   <element ype="string" column="school_name"/>         
  4.  </bag>  
<bag name="school" table="schools" >       
  <key column="pid" not-null="true" />       
  <element ype="string" column="school_name"/>       
 </bag>

 

4. Map 集合属性
Map 不公需要映射属性值,还需要映射属性 Key 。映射 Map 集合属性时,同样需要指定外键列,同时还必须指定 MapKey 列。显然,系统将以外键列和 Key 列作为联合主键。
与所有集合属性类似,属性声明只能使用接口,看下面的 POJO 类:

Java代码 复制代码 收藏代码
  1. private Map school=new  HashMap();     
  2.           
  3. public Map getSchool() {     
  4.     return school;     
  5. }     
  6. public void setSchool(Map school) {     
  7.    this.school=school;     
  8. }  
private Map school=new  HashMap();   
        
public Map getSchool() {   
    return school;   
}   
public void setSchool(Map school) {   
   this.school=school;   
}

 

Map 集合属性使用 map 元素映射时,该 map 元素需要 keymap-key 两个子元素。其中 key 子元素用于映射外键列,而 map-key 子元素则用于映射 Map 集合的 Key 。映射文件如下:

Xml代码 复制代码 收藏代码
  1. <map name="school" table="schools">  
  2.       <key column="pid" not-null="true" />       
  3.       <map-key type="string" column="indet"/>     
  4.       <element type="float" column="score"/>     
  5.  </map>   
<map name="school" table="schools">
      <key column="pid" not-null="true" />     
      <map-key type="string" column="indet"/>   
      <element type="float" column="score"/>   
 </map> 

 

注意:map-keyelement 元素都必须确定type属性

5. 集合性能的分析
对于集合属性,通常推荐使用延迟加载策略。所谓延迟加载就是当系统需要使用集合属性时才从数据库装载关联的数据。
Hibernate 对集合属性默认采用延迟加载,在某些特殊的情况下为 set,,list,map 等元素设置lazy="false" 属性来取消延迟加载。
可将集合分成如下两类:

  • 有序集合:集合里的元素 可以根据 KeyIndex 访问
  • 无序集合:集合里的元素中能遍历

有序集合的属性有增加、删除及修改中拥有较好的性能表现。在设计较好的 Hiberate domain Object 中,集合属性通常都会增加 inverse="true" 的属性,此时集合端不再控制关联关系。映射 Set 集合属性时,如果 element 元素包括 not-null="true" 属性,则集合属性表以关联持久化类的外键和元素列作为联合主键,否则该表没有主键,但 List 集合属性的表总是以外键和元素次序列作为联合主键。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics