1.

How many types of collection are there in Hibernate? Explain Collection Mapping?

Answer»

The HIBERNATE collections BEHAVE like HashMap, HashSet, TreeMap, TreeSet or ArrayList. Collection instances are like value types and are automatically persisted when referenced by a persistent object and deleted when unreferenced. There are five types of collections in Hibernate. They are: 

  1. Map  
  2. Array 
  3. List 
  4. Set 
  5. Bag

To map these collections, the type of collection must be declared from one of the following: 

  • java.util.List 
  • java.util.Set 
  • java.util.SortedSet 
  • java.util.Map 
  • java.util.SortedMap 
  • java.util.Collection 
  • or write the implementation of org.hibernate.usertype.UserCollectionType

The Hibernate mapping element used for mapping a collection depends UPON the type of INTERFACE. The basic syntax for mapping is: 

<hibernate-mapping>  <class name="<event.Class>" table="<TableName>">            <id name="id" column="<ColumnName>">                     <generator class="native"/>            </id>           <property name="<property>" type="<type>" column="<ColumnName>"/>           <property name="<property>"/>          <set name = “<name of set>”>  <key column=“<column name>” not-null= “true”/>                         <one-to-many class=“<associated class>”/>          </set>  </class>  </hibernate-mapping> 

The <set> can be replaced with map, list etc. There are different associations for mapping the class with the CORRESPONDING table. The types of associations are as below: 

  1. one-to-many 
  2. one-to-one 
  3. many-to-one 
  4. many-to-many 

When using a map for mapping, a map-key has to be provided along with the key column.  

<map name="developmentLanguages" table="tdevelopmentlanguage" cascade="all">           <key column="developer_id" not-null="true"></key>           <map-key type="string" column="shortname"></map-key>          <element column="name" type="string"></element>  </map>


Discussion

No Comment Found