Tuesday, October 18, 2011

Mapping Collections in Hibernate using Map interface

Mapping a Map in Hibernate:

To implement the Map interface, we need to provide an additional column in the collections table to support the key elements of a Map.

Mapping using Hibernate XML mapping:

Let us go to the package
com.demo.collections.Map and execute the class MapExecutor.java

Things to look into:

*
The ORM class Exam.java
*
The xml mapping file com.demo.collection.Map.hbm.xml

ORM:

Here the variable timeTable has been declared using the Map interface.

public class Exam {

private long id;

private String examName;

private Map timeTable = new HashMap();

// getter and setter methods
}


XML mapping file:

<hibernate-mapping auto-import="false">
<class name="com.demo.collections.Map.Exam" table="EXAMS" lazy="false">
<id name="id" column="EXAM_ID">
<generator class="native" />
</id>
<property name="examName" column="EXAM_NAME" />
<map name="timeTable" table="TIME_TABLE" lazy="false">
<key column="EXAM_ID" />
<map-key column="SUBJECT" type="string" />
<element column="EXAM_DATE" type="date" />
</map>
</class>
</hibernate-mapping>


*
The xml element <map> specifies the map interface and the table TIME_TABLE is to store the collection values.

* The xml element key specifies that the primary key of the table EXAMS will be the foreign key in the table TIME_TABLE.

* The xml element element represents the column in the collections table to store the collection values.

* The xml element <map-key> specifies the column in the collections table that will store the key elements of a map.

The primary key of this collections table will be the combination of key and map-key pairs.

After executing MapExecutor.java, run the simple select queries on the tables EXAMS and TIME_TABLE and verify the outputs in the eclipse console as well as follows.


Mapping Map using Annotations:

Let us go to the package annotation.com.demo.collections.Map and execute the class MapAnnotationExecutor.java

Things to look into:

*
The annotated ORM Exam.java

@Entity
@Table(name="EXAMS")
public class Exam {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="EXAM_ID")
private long id;

@Column(name="EXAM_NAME")
private String examName;

@org.hibernate.annotations.CollectionOfElements(targetElement=java.util.Date.class,fetch=FetchType.EAGER)
@JoinTable(name="TIME_TABLE", joinColumns=@JoinColumn(name="EXAM_ID"))
@org.hibernate.annotations.MapKey(columns=@Column(name="SUBJECT"))
@Column(name="EXAM_DATE")
private Map timeTable = new HashMap();

* The hibernate annotation @org.hibernate.annotations.CollectionOfElements indicates that the variable timeTable is of the type collections.
* The targetElement represents the type of element that collection stores and not required if we use the java Generics.
* The annotation @JoinTable is used to mention the table that is used to store the collection values.
* The annotation @JoinColumn here is equivalent to the xml element <key> as mentioned above.
* The hibernate annotation @org.hibernate.annotations.MapKey specifies the column that will store the key elements of a Map.

After executing the MapAnnotationExecutor.java, verify the results in the console and in the HSQL DB manager as mentioned above.