Using Hibernate with ZK"

From Documentation
m
Line 9: Line 9:
 
===The Required Libraries===
 
===The Required Libraries===
  
To get started the following libraries are required for a ZK Hibernate application with HSQLDB. When testing out the sample application provided with this book chapter you can ignore the below details as the libraries are provided in the WAR file along with the sample. For those of you who would like to start your own project you will require the following libraries.
+
To get started the following libraries are required for a ZK Hibernate application with HSQLDB. When testing out the sample application provided with this book chapter you can ignore the below details as the libraries are provided in the WAR file along with the sample. For those of you who would like to start your own project the essential libraries required are.
  
 
*Hibernate 3.6
 
*Hibernate 3.6
 
*ZK 5.0.5
 
*ZK 5.0.5
 
*HSQLDB jar
 
*HSQLDB jar
*Log4j jar
 
  
 
Here is a screenshot of the jar files found in the sample application for this chapter.
 
Here is a screenshot of the jar files found in the sample application for this chapter.

Revision as of 01:32, 10 November 2010

Stop.png This article is out of date, please refer to http://books.zkoss.org/zkessentials-book/master/ for more up to date information.


As established previously, the applications DAOs access an "in-memory" model provided by Java Lists. We are required to adjust the DAOs so they access a persistent model powered by HSQLDB using Hibernate.

Hibernate is an ORM framework of choice for many large enterprises as it provides an easy way to access persistent storage. Hibernate can be combined with Spring and EJB3 to make the mangement of application lifecycles and session trivial providing greater flexibility for developers. In this example only Hibernate will be used, however, there are many examples of using Hibernate along with Spring available on the web.

HSQLDB is a relational database engine written in Java which provides an easy way to persist data to a file store.

The Required Libraries

To get started the following libraries are required for a ZK Hibernate application with HSQLDB. When testing out the sample application provided with this book chapter you can ignore the below details as the libraries are provided in the WAR file along with the sample. For those of you who would like to start your own project the essential libraries required are.

  • Hibernate 3.6
  • ZK 5.0.5
  • HSQLDB jar

Here is a screenshot of the jar files found in the sample application for this chapter.

ZKEss all libs.png

Setting up Hibernate

Hibernate requires configuration to work. Our sample application contains two configuration files placed in folder included in the classpath:

1. log4j.properties 2. hibernate.cfg.xml

The log4j.properties file is used to inform Log4j of the level that we wish to log. In our case we set the lowest level of logging possible which in this case is DEBUG and TRACE for the hibernate classes. Setting this to such a fine grained setting enables us to see exactly what is going on in hibernate.

The second file hibernate.cfg.xml contains detailed configuration for Hibernate in XML format. The content is provided and analyzed below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="hibernate.connection.url">jdbc:hsqldb:file:data/store</property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>

    
    <property name="hibernate.hbm2ddl.auto">create-drop</property>
    
    <mapping class="demo.model.bean.Order"/>
    <mapping class="demo.model.bean.OrderItem"/>
    <mapping class="demo.model.bean.User"/>
    <mapping class="demo.model.bean.Product"/>
  </session-factory>
</hibernate-configuration>
  • Line 5 let’s hibernate know that we intend to use HSQLDialect as it is an HSQL database
  • Line 6 specifies the jdbc driver class
  • Line 7 is the path to the datastore, in this case informing HSQLDB we will make use of a file named store in the folder data
  • Line 8 is the username, in this case a default sa
  • Line 9 dictates that we would like Hibernate to print out the SQL
  • Line 10 states that the session context is thread based
  • Line 11 states that the HQL style we would like to use is classic
    • HQL is Hibernates own query language for more information please click here
  • Lines 14-19 are to do with definition of the entity types within the application and generation of the database. These topics are covered in the following section.



Last Update : 2010/11/10

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