Tuesday, October 18, 2011

Mapping Collections in Hibernate using Set interface

Mapping a Set in Hibernate:

If an entity or class has collection of values for a particular variable, then we can map those values using any one of the collection interfaces available in java. Here in this case we will be using Set interface. We need an additional table to store these collection values.

Mapping using Hibernate XML mapping:

Let us go to the package com.demo.collections.Set in the sample application, and execute the java class SetExecutor.java.

Things to look into:

* The ORM class (Folder.java)
* XML mapping file (com.demo.collections.Set.hbm.xml)


ORM:
The Folder.java has three variables and the variable files is of the type collections.

public class Folder {

private long id;

private String folderName;

private Set files = new HashSet();

// getter and setter methods

}

XML mapping file:

<hibernate-mapping auto-import="false">
<class name="com.demo.collections.Set.Folder" table="FOLDERS" lazy="false">
<id name="id" column="FOLDER_ID" type="long">
<generator class="native" />
</id>
<property name="folderName" column="FOLDER_NAME" type="string" />
<!-- Declaration for Set -->
<set name="files" table="FILES" lazy="false">
<key column="FOLDER_ID"></key>
<element type="string" column="FILENAME" not-null="true"></element>
</set>
</class>
</hibernate-mapping>

* The Set has been mentioned in a separate xml element and a table "FILES" has been included to store the collection values.
* The xml element key specifies that the primary key of the table FOLDERS will be the foreign key in the table FILES.
* The xml element element represents the column in the collections table FILES that will actually contain the collection values.

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

After executing the
SetExecutor.java, verify the output in the console window as well as in the HSQL DB manager. Run the following simple select queries on the tables FOLDERS and FILES and expect the results as shown below.




Mapping Collections using Annotations:

Let us now go to the package annotation.com.demo.collections.Set and execute the java class SetAnnotationExecutor.java.

Things to look into:

* The annotated ORM (Folder.java)

@Entity
@Table(name="FOLDERS")
public class Folder {

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

@Column(name="FOLDER_NAME")
private String folderName;

@org.hibernate.annotations.CollectionOfElements(
targetElement=java.lang.String.class,fetch=FetchType.EAGER )
@JoinTable(name="FILES", joinColumns=@JoinColumn(name="FOLDER_ID"))
@Column(name="FILENAME", nullable=false)
private Set
<String> files = new HashSet<String>();

* The hibernate annotation
@org.hibernate.annotations.CollectionOfElements says that the variable "files" 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.

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