Hibernate

From Documentation

Overview

Hibernate is an object-relational mapping (ORM) solution for the Java language. The main feature of Hibernate is that it simplifies the job of accessing a database.

Example here we use with Hibernate 3.3 and hsqldb 1.8. It shall work with the newer version.

Installing Hibernate

Before using Hibernate, you have to install it into your application first.

  1. Download hibernate core and hibernate annotations from Hibernate
  2. Put *.jar files from hibernate core and hibernate annotations into your $myApp/WEB-INF/lib/

$myApp represents the name of your web application. ex. event

Configuring the ZK Configuration File

To make ZK works with Hibernate smoothly, you have to use the following utilities.

  1. Create zk.xml under $myApp/WEB-INF/(if not exists)
  2. Copy the following lines into your zk.xml
 <!-- Hibernate SessionFactory lifecycle -->
 <listener>
 <description>Hibernate SessionFactory lifecycle</description>
 <listener-class>org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener</listener-class>
 </listener>

 <!-- Hibernate OpenSessionInView Pattern -->
 <listener>
 <description>Hibernate Open Session In View life-cycle</description>
 <listener-class>org.zkoss.zkplus.hibernate.OpenSessionInViewListener</listener-class>
 </listener>

 <!-- Hibernate thread session context handler -->
 <listener>
 <description>Hibernate thread session context handler</description>
 <listener-class>org.zkoss.zkplus.hibernate.HibernateSessionContextListener</listener-class>
 </listener>

$myApp represents the name of your web application. ex. event

Creating the Java Objects

You have to create simple JavaBean class with some properties.

  1. Create your first Java class (Event.java)
 package events;

 import java.util.Date;

 public class Event {
     private Long id;
     private String title;
     private Date date;

     public Event() {}
         public Long getId() {
             return id;
     }
     private void setId(Long id) {
         this.id = id;
     }
     public Date getDate() {
         return date;
     }
     public void setDate(Date date) {
         this.date = date;
     }
     public String getTitle() {
         return title;
     }
     public void setTitle(String title) {
         this.title = title;
     }
 }
  1. You have to compile the Java source, and place the class file in a directory called classes in the Web development folder, and in its correct package. (ex.$myApp/WEB-INF/classes/event/Event.class)

The next step is to tell Hibernate how to map this persistent class with database.

Mapping the Java Objects

There are two ways to tell Hibernate how to load and store objects of the persistent class, one is using Hibernate mapping file, and the other is using Java Annotation.

Using the Mapping Files

  1. Simply create Event.hbm.xml for the persistent class Event.java.
 <?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "[http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]">

 <hibernate-mapping>
     <class name="events.Event" table="EVENTS">
         <id name="id" column="EVENT_ID">
             <generator class="native"/>
         </id>
             <property name="date" type="timestamp" column="EVENT_DATE"/>
             <property name="title"/>
     </class>
 </hibernate-mapping>
  1. Place this Event.hbm.xml in the directory called src in the development folder, and its correct package. (ex.$myApp/WEB-INF/src/event/Event.hbm.xml)

Using Java Annotation

The benefit of using Java annotation instead of Hibernate mapping file is that no additional file is required. Simply add Java annotation on your Java class to tell Hibernate about the mappings.

 package events;

 import java.util.Date;

 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.Table;

 @Entity
 @Table(name="EVENTS")
 public class Event {
     private Long id;
     private String title;
     private Date date;

     @Id
     @GeneratedValue(strategy=GenerationType.SEQUENCE)
     @Column(name = "EVENT_ID")
     public Long getId() {
         return id;
     }
     private void setId(Long id) {
         this.id = id;
     }
     @Column(name = "EVENT_DATE")
     public Date getDate() {
         return date;
     }
     public void setDate(Date date) {
         this.date = date;
     }
     public String getTitle() {
         return title;
     }
     public void setTitle(String title) {
         this.title = title;
     }
 }
  • @Entity declares this class as a persistence object
  • @Table(name = "EVENTS") annotation tells that the entity is mapped with the table EVENTS in the database
  • @Column element is used to map the entities with the column in the database.
  • @Id element defines the mapping from that property to the primary key column.

Creating the Hibernate Configuration File

Creating DAO Objects

Accessing Persistence Objects in ZUML Page

Version History

Last Update : 2010/11/17


Version Date Content
     



Last Update : 2010/11/17

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.