Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Collection Mapping


We can map collection elements of Persistent class in Hibernate. You need to declare the type of collection in Persistent class from one of the following types:

  • 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 persistent class should be defined like this for collection element.

  1. package com.Kreationnext;
  2. import java.util.List;
  3. public class Question {
  4. private int id;
  5. private String qname;
  6. private List<String> answers;//List can be of any type
  7. //getters and setters
  8. }

Mapping collection in mapping file

There are many subelements of <class> elements to map the collection. They are <list>, <bag>, <set> and <map>. Let’s see how we implement the list for the above class:

  1. <class name=“com.Kreationnext.Question” table=“q100”>
  2.           <id name=“id”>
  3.           <generator class=“increment”></generator>
  4.           </id>
  5.           <property name=“qname”></property>
  6.           <list name=“answers” table=“ans100”>
  7.           <key column=“qid”></key>
  8.           <index column=“type”></index>
  9.           <element column=“answer” type=“string”></element>
  10.           </list>
  11.           </class>

There are three subelements used in the list:

  • <key> element is used to define the foreign key in this table based on the Question class identifier.
  • <index> element is used to identify the type. List and Map are indexed collection.
  • <element> is used to define the element of the collection.

This is the mapping of collection if collection stores string objects. But if collection stores entity reference (another class objects), we need to define <one-to-many> or <many-to-many> element. Now the Persistent class will look like:

  1. package com.Kreationnext;
  2. import java.util.List;
  3. public class Question {
  4. private int id;
  5. private String qname;
  6. private List<Answer> answers;//Here, List stores the objects of Answer class
  7. //getters and setters
  8. }
  1. package com.Kreationnext;
  2. import java.util.List;
  3. public class Answer {
  4. private int id;
  5. private String answer;
  6. private String posterName;
  7. //getters and setters
  8. }

Now the mapping file will be:

  1. <class name=“com.Kreationnext.Question” table=“q100”>
  2.           <id name=“id”>
  3.           <generator class=“increment”></generator>
  4.           </id>
  5.           <property name=“qname”></property>
  6.           <list name=“answers” >
  7.           <key column=“qid”></key>
  8.           <index column=“type”></index>
  9.           <one-to-many class=“com.Kreationnext.Answer” />
  10.           </list>
  11.           </class>

Here, List is mapped by one-to-many relation. In this scenario, there can be many answers for one question.


Understanding key element

The key element is used to define the foreign key in the joined table based on the original identity. The foreign key element is nullable by default. So for non-nullable foreign key, we need to specify not-null attribute such as:

  1. <key column=“qid” not-null=“true” ></key>

The attributes of the key element are column, on-delete, property-ref, not-null, update and unique.

  1. <key
  2. column=“columnname”
  3. on-delete=“noaction|cascade”
  4. not-null=“true|false”
  5. property-ref=“propertyName”
  6. update=“true|false”
  7. unique=“true|false”
  8. />

Indexed collections

The collection elements can be categorized in two forms:

  • indexed ,and
  • non-indexed

The List and Map collection are indexed whereas set and bag collections are non-indexed. Here, indexed collection means List and Map requires an additional element <index>.


Collection Elements

The collection elements can have value or entity reference (another class object). We can use one of the 4 elements

  • element
  • component-element
  • one-to-many, or
  • many-to-many

The element and component-element are used for normal value such as string, int etc. whereas one-to-many and many-to-many are used to map entity reference.


Comments

comments