Using Hibernate with ZK"

From Documentation
m (Redirected page to ZK Essentials)
 
Line 1: Line 1:
 +
#REDIRECT [[ZK Essentials]]
 +
 
{{ZKEssentialsPageHeader}}
 
{{ZKEssentialsPageHeader}}
  

Latest revision as of 00:05, 29 March 2013

Redirect to:

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. It is required to adjust the DAOs so they can access a persistent model powered by HSQLDB using Hibernate.

Hibernate is an ORM framework of choices 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 lifecycle management of applications and session trivial greater flexibility for developers. In this example only Hibernate will be used, however, there are many other examples of using Hibernate along with Spring available on the web.

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

Setting up Hibernate

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

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

The log4j.properties file is used to inform Log4j of the level 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 during 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 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 that 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 : 2013/03/29

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