Hibernate"

From Documentation
(Created page with '{{ZKDevelopersReferencePageHeader}} =Overview= Hibernate is an object-relational mapping (ORM) solution for the Java language. The main feature of Hibernate is that it simplifie…')
 
Line 43: Line 43:
  
 
=Creating the Java Objects=
 
=Creating the Java Objects=
 +
You have to create simple JavaBean class with some properties.
 +
 +
# Create your first Java class (<tt>Event.java</tt>)
 +
 +
<source lang="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;
 +
    }
 +
}
 +
</source>
 +
 +
# You have to compile the Java source, and place the class file in a directory called <tt>classes</tt> in the Web development folder, and in its correct package. (ex.<tt>$myApp/WEB-INF/classes/event/Event.class</tt>)
 +
 +
The next step is to tell Hibernate how to map this persistent class with database.
 +
 
=Mapping the Java Objects=
 
=Mapping the Java Objects=
 
=Creating the Hibernate Configuration File=
 
=Creating the Hibernate Configuration File=

Revision as of 11:32, 17 November 2010

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

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.