Hibernate Collection Mapping

Entities can have variable which can have collection of values, we can map those values using collecion interfaces provided in java. Hibernate can persist instances of java.util.Map, java.util.Set, java.util.SortedMap, java.util.SortedSet, java.util.List etc.

Map Collections:

You can map collections,lists,sets and maps using @OneToMany and @ManyToMany annotations. For collections of a basic or embeddable type use @ElementCollection.

-->java.util.List

-->java.util.Set

-->java.util.SortedSet

-->java.util.Collection

-->java.util.Map

-->java.util.SortedMap

For any of the above collection we have specific subelements of class element which needs to be defined. They are <list>, <bag>, <set> and <map>.

e.g Mapping between Person and Country Table:

Persistent Class:

    package com.javasafari;  
      
    import java.util.List;  
      
    public class Person {  
    private int id;  
    private String pname;  
    private List<String> cntryVisited;//List can be of any type  
      
    //getters and setters  
      
    }  

Mapping file for above class:

     <class name="com.javasafari.Person" table="Person">  
              <id name="id" type="int" column="id">
         <generator class="native"/>
              </id>  
              <property name="pname" column="person_name" type="string"></property>  
                
              <list name="cntryVisited" table="Country" cascade="all">  
              <key column="pid"></key>  
              <list-index column="idx"></list-index>  
              <element column="country_name" type="string"></element>  
              </list>  
                
              </class>  

Three subelements used in the list:

<key> element is used to define the key in this table based on the Person class identifier. To map Person and Country table.

<index> element is used to identify the list index.

<element> is used to define the element of the collection.

This mapping of collection is used if collection stores string objects. But if collection stores entity reference then we need to define <one-to-many> or <many-to-many> element.

e.g Persistent Class

    package com.javasafari;  
      
    import java.util.List;  
      
    public class Person {  
    private int id;  
    private String pname;  
    private List<Country> cntryVisited;//List can be of any type  
      
    //getters and setters  
      
    }  

    package com.javasafari;  
    
    import java.util.List;  
    
    public class Country {  
    
    private int id;  
    private String countryName;  
    
    //getters and setters  
    
    } 
    

Mapping file for above class:

     <class name="com.javasafari.Person" table="Person">  
              <id name="id" type="int" column="id">
         <generator class="native"/>
              </id>  
              <property name="pname" column="person_name" type="string"></property>  
                
              <list name="cntryVisited" cascade="all">  
              <key column="pid"></key>  
              <list-index column="idx"><list-/index>  
             <one-to-many class="com.javatsafari.Country" />  
              </list>  
                
              </class>  

<class name="com.javasafari.Country" table="Country">
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="countryName" column="country_name" type="string"/>
   </class>

Collection Category:

Collection can be categorized in two category:

1) Indexed- List and Map collection are indexed.

2) Non Indexed- set and bag collections are non-indexed.