Hibernate"

From Documentation
m (remove empty version history (via JWB))
 
(14 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
= Overview =
 
= Overview =
  
Due to ''object/relational paradigm mismatch'' <ref> Hibernate in Action, Christian Bauer, Gavin King, Manning</ref>, developers tend to use ORM (''object/relational mapping'') framework to convert object-oriented model to relational model and vice versa. ''Hibernate'' is the most popular ORM framework in Java world. We will talk about some integration topics in this chapter such as lazy initialization with Spring. If you haven't read about basic concepts and installation of Hibernate, please refer to [http://www.hibernate.org/docs Hibernate Dcumentation].
+
Due to ''object/relational paradigm mismatch'' <ref> Hibernate in Action, Christian Bauer, Gavin King, Manning</ref>, developers tend to use ORM (''object/relational mapping'') framework to convert object-oriented model to relational model and vice versa. ''Hibernate'' is the most popular ORM framework in Java world. We will talk about some integration topics in this chapter such as lazy initialization with Spring. If you haven't read about basic concepts and installation of Hibernate, please refer to [http://www.hibernate.org/docs Hibernate Dcumentation]. The example we give in this chapter is based on Hibernate '''4.0.0.final''' and Spring '''3.1.2.RELEASE'''.
  
 
= Integrate with Different DAO implementation =
 
= Integrate with Different DAO implementation =
Line 13: Line 13:
 
According to Hibernate Reference Manual's suggestion <ref> [http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#transactions-basics-uow Unit of Work in Hibernate Core Reference Manual]</ref>, we should apply ''session-per-request'' pattern to manage sessions and transactions. This pattern needs an interceptor to open a contextual session with a transaction  when a request is going to be handled and close the session before respond is sent to client (aka. ''open session in view'' pattern). A common implementation for page-based application is a servlet filter.  
 
According to Hibernate Reference Manual's suggestion <ref> [http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#transactions-basics-uow Unit of Work in Hibernate Core Reference Manual]</ref>, we should apply ''session-per-request'' pattern to manage sessions and transactions. This pattern needs an interceptor to open a contextual session with a transaction  when a request is going to be handled and close the session before respond is sent to client (aka. ''open session in view'' pattern). A common implementation for page-based application is a servlet filter.  
  
Applying this pattern also solves a common "LazyInitializationException" problem that most developers encounter when using lazy fetching strategy. In brief, Hibernate session is usually closed after a DAO object has performed an operation (a.k.a ''session-per-operation'' pattern). Those persistent objects become ''detached'' after the associated sessions are closed. If we access a detached object's lazy-loaded collection when rendering the view. We will get an error message like <tt> LazyInitializationException: no session or session was closed</tt>. For detailed explanation, please refer to the article "Open Session in View" on Hibernate community<ref> [https://community.jboss.org/wiki/OpenSessionInView#The_problem Open Session in View\Problem]</ref>. As we apply ''session-per-request'' pattern, a Hibernate session is kept open when a View is accessing lazy-loaded collection. Those objects queried by the Hibernate session becomes detached later (after the interceptor closes the Hibernate session), so the previously mentioned problem is resolved.
+
Applying this pattern also solves a common "LazyInitializationException" problem that most developers encounter when using lazy fetching strategy. In brief, Hibernate session is usually closed after a DAO object has performed an operation (a.k.a ''session-per-operation'' pattern). Those persistent objects become ''detached'' after the associated sessions are closed. If we access a detached object's lazy-loaded collection when rendering the view. We will get an error message like <code>LazyInitializationException: no session or session was closed</code>. For detailed explanation, please refer to the article "Open Session in View" on Hibernate community<ref> [https://community.jboss.org/wiki/OpenSessionInView#The_problem Open Session in View\Problem]</ref>. As we apply ''session-per-request'' pattern, a Hibernate session is kept open when a View is accessing lazy-loaded collection. Those objects queried by the Hibernate session becomes detached later (after the interceptor closes the Hibernate session), so the previously mentioned problem is resolved.
  
  
 
== Homemade DAO==
 
== Homemade DAO==
Here we introduce how to implement an DAO without using other frameworks (e.g. Spring).
+
Here we introduce how to implement an DAO without other frameworks (e.g. Spring).
 +
 
 +
=== Configuration ===
 +
The minimal Maven dependency you need is:
 +
 
 +
<source lang='xml'>
 +
 
 +
    <dependency>
 +
      <groupId>org.hibernate</groupId>
 +
      <artifactId>hibernate-core</artifactId>
 +
      <version>4.0.0.Final</version>
 +
      <scope>compile</scope>
 +
    </dependency>
 +
 
 +
</source>
 +
 
 +
<div style="-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px;-moz-background-clip:padding;-webkit-background-clip:padding-box;background-clip:padding-box;color:#c06330;padding:15px 40px;background:#fed no-repeat 13px 13px;margin-bottom:10px">
 +
[[File:Icon_info.png ]] '''Note:''' If you don't use Maven, please refer to Hibernate Reference Documentation to know which JAR file you need.
 +
</div>
 +
 
  
 
=== Utility Class ===
 
=== Utility Class ===
  
A simple way to implement a DAO is to control Hibernate sessions and transactions manually, hence we need a utility class to get <tt>SessionFactory</tt>. ZK's <javadoc>org.zkoss.zkplus.hibernate.HibernateUtil</javadoc> has been deprecated since 6.0.2, you can write your own one according to the code example in Hibernate Reference Manual.<ref> [http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#tutorial-firstapp-helpers Hibernate Reference Documentation\ Tutorial] </ref> Here we provide a simple example.
+
A simple way to implement a DAO is to control Hibernate sessions and transactions manually, hence we need a utility class to get <code>SessionFactory</code>. ZK's <javadoc>org.zkoss.zkplus.hibernate.HibernateUtil</javadoc> has been deprecated since 6.0.2, you can write your own one according to the code example in Hibernate Reference Manual.<ref> [http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#tutorial-firstapp-helpers Hibernate Reference Documentation\ Tutorial] </ref> Here we provide a simple example.
  
 
'''Utility class to get SessionFactory'''
 
'''Utility class to get SessionFactory'''
Line 49: Line 68:
 
=== An Open Session Listener ===
 
=== An Open Session Listener ===
  
For ''open session in view'' pattern, we need an interceptor to open sessions. In ZK, we need to intercept all requests including AU requests, so we implement ZK's [[ZK Developer's Reference/Customization/Init and Cleanup| execution init and cleanup listener]] to achieve this. (Our listener's implementation is based on the filter mentioned by a Hibernate article "Open Session in View". <ref> [https://community.jboss.org/wiki/OpenSessionInView#Using_an_interceptor Open Session in View\Using an intercepto]</ref>.) This listener opens a session and begin a transaction at the beginning of an execution (ZK's http request wrapper) and commit (or rollback) at the end of the execution.
+
For ''open session in view'' pattern, we need an interceptor to open sessions. In ZK, we need to intercept all requests including AU requests, so we implement ZK's [[ZK Developer's Reference/Customization/Life Cycle Listener| Life Cycle Listener]] to achieve this. (Our listener's implementation is based on the filter mentioned by a Hibernate article "Open Session in View". <ref> [https://community.jboss.org/wiki/OpenSessionInView#Using_an_interceptor Open Session in View\Using an intercepto]</ref>.) This listener opens a session and begins a transaction at the beginning of an execution (ZK's HTTP request wrapper, then commits (or rollback) at the end of the execution.
  
 
'''Extracted from OpenSessionInViewListener'''
 
'''Extracted from OpenSessionInViewListener'''
<source lang="java" high="7, 15,18">
+
<source lang="java" highlight="7, 15,18">
  
 
public class OpenSessionInViewListener implements ExecutionInit, ExecutionCleanup {
 
public class OpenSessionInViewListener implements ExecutionInit, ExecutionCleanup {
Line 88: Line 107:
 
}
 
}
 
</source>
 
</source>
* Line 7: Call <tt>getCurrentSession()</tt> to get a contextual session. <ref> [http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#architecture-current-session Hibernate Reference Documentation/contextual session]</ref>
+
* Line 7: Call <code>getCurrentSession()</code> to get a contextual session. <ref> [http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#architecture-current-session Hibernate Reference Documentation/contextual session]</ref>
  
 
Add configuration in zk.xml to make the listener work.
 
Add configuration in zk.xml to make the listener work.
Line 106: Line 125:
  
 
'''Simple DAO implementation'''
 
'''Simple DAO implementation'''
<source lang="java" high="4">
+
<source lang="java" highlight="4">
 
public class OrderDao {
 
public class OrderDao {
  
Line 157: Line 176:
 
==Spring-based DAO ==
 
==Spring-based DAO ==
  
With Spring provided dependency injection and ORM support, here our efforts are simplified quite substantially. We'll demonstrate one typical usage in non-managed environment. To apply ''session-per-request'' pattern, we can use Spring provided <tt>OpenSessionInViewFilter</tt> instead of writing our own one. According to the suggestion in Spring Reference Documentation 3.1, using plain Hibernate API to implement a DAO is the current recommended usage pattern. In the DAO, we can also easily retrieve <tt>SessionFactory</tt> by dependency inject without any utility classes (<tt>HibernateUtil</tt>). Besides, declarative transaction management and rollback rule also reduces our work. The following are the the related code snippets.
+
With Spring provided dependency injection and ORM support, here our efforts are simplified quite substantially. We'll demonstrate one typical usage in non-managed environment. To apply ''session-per-request'' pattern, we can use Spring provided <code>OpenSessionInViewFilter</code> instead of writing our own one. According to the suggestion in Spring Reference Documentation 3.1, using plain Hibernate API to implement a DAO is the current recommended usage pattern. In the DAO, we can also easily retrieve <code>SessionFactory</code> by dependency inject without any utility classes (<code>HibernateUtil</code>). Besides, declarative transaction management and rollback rule also reduces our work. The following are the the related code snippets.
 +
 
 +
=== Configuration ===
 +
The minimal dependencies you need are Hibernate-Core, Spring-Web, Spring-ORM, and Cglib:
 +
 
 +
<source lang='xml'>
 +
 
 +
  <dependency>
 +
  <groupId>org.hibernate</groupId>
 +
  <artifactId>hibernate-core</artifactId>
 +
  <version>4.0.0.Final</version>
 +
  </dependency>
 +
  <dependency>
 +
  <groupId>org.springframework</groupId>
 +
  <artifactId>spring-web</artifactId>
 +
  <version>3.1.2.RELEASE</version>
 +
  </dependency>
 +
  <dependency>
 +
  <groupId>org.springframework</groupId>
 +
  <artifactId>spring-orm</artifactId>
 +
  <version>3.1.2.RELEASE</version>
 +
  </dependency>
 +
  <dependency>
 +
  <groupId>cglib</groupId>
 +
  <artifactId>cglib</artifactId>
 +
  <version>2.2</version>
 +
  </dependency>
 +
</source>
  
 
We use basic configuration for Spring.
 
We use basic configuration for Spring.
  
'''Spring configuration'''
+
'''Spring configuration for Hibernate'''
<source lang="xml">
+
<source lang="xml" highlight='40'>
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 +
xmlns:context="http://www.springframework.org/schema/context"
 +
xmlns:tx="http://www.springframework.org/schema/tx"
 +
xsi:schemaLocation="
 +
          http://www.springframework.org/schema/beans
 +
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 +
          http://www.springframework.org/schema/context
 +
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
 +
          http://www.springframework.org/schema/tx
 +
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  
 
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
 
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<!-- data source properties -->
+
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
 +
<property name="url" value="jdbc:hsqldb:file:data/store" />
 +
<property name="username" value="sa" />
 +
<property name="password" value="" />
 
</bean>
 
</bean>
 
+
<!--
 +
hibernate.current_session_context_class=thread
 +
-->
 
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
 
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
 
<property name="dataSource" ref="dataSource" />
 
<property name="dataSource" ref="dataSource" />
<!-- hibernate related properties -->
+
<property name="hibernateProperties">
 +
<value>
 +
hibernate.dialect=org.hibernate.dialect.HSQLDialect
 +
hibernate.hbm2ddl.auto=crate
 +
hibernate.show_sql=true
 +
hibernate.connection.pool_size=5
 +
hibernate.connection.autocommit=false
 +
</value>
 +
</property>
 +
<property name="annotatedClasses">
 +
<list>
 +
<value>org.zkoss.reference.developer.hibernate.domain.Order</value>
 +
<value>org.zkoss.reference.developer.hibernate.domain.OrderItem</value>
 +
</list>
 +
</property>
 
</bean>
 
</bean>
 
 
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
 
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
 
<property name="sessionFactory" ref="sessionFactory" />
 
<property name="sessionFactory" ref="sessionFactory" />
Line 179: Line 254:
 
<tx:annotation-driven />
 
<tx:annotation-driven />
 
 
 +
<!-- Scans for application @Components to deploy -->
 
<context:component-scan base-package="org.zkoss.reference.developer.hibernate" />
 
<context:component-scan base-package="org.zkoss.reference.developer.hibernate" />
 +
</beans>
  
 
</source>
 
</source>
* For Hibernate 3.x, some package names should be changed to <tt> org.springframework.orm.hibernate3.*</tt>, e.g. <tt>org.springframework.orm.hibernate3.HibernateTransactionManager</tt>.
+
* Line 40: For Hibernate 3.x, some package names should be changed to <code>org.springframework.orm.hibernate3.*</code>, e.g. <code>org.springframework.orm.hibernate3.HibernateTransactionManager</code>.
  
 
=== OpenSessionInViewFilter ===
 
=== OpenSessionInViewFilter ===
Line 189: Line 266:
  
 
''' Configure OpenSessionInViewFilter in web.xml'''
 
''' Configure OpenSessionInViewFilter in web.xml'''
<source lang="xml" high="3">
+
<source lang="xml" highlight="3">
  
 
<filter>
 
<filter>
Line 204: Line 281:
 
=== DAO Implementation ===
 
=== DAO Implementation ===
  
For a Spring-powered DAO, we can use injected <tt>SessionFactory</tt> and <tt>@Transactional</tt> to perform persistence operation.
+
For a Spring-powered DAO, we can use injected <code>SessionFactory</code> and <code>@Transactional</code> to perform persistence operation.
  
 
'''DAO empowered by Spring'''
 
'''DAO empowered by Spring'''
<source lang="java" high="4,5,7,9,15,23">
+
<source lang="java" highlight="4,5,7,9,15,23">
  
 
@Repository
 
@Repository
Line 254: Line 331:
 
= Lazy Initialization Issue among AU Requests =
 
= Lazy Initialization Issue among AU Requests =
  
Although we apply ''open session in view'' pattern to keep a session open when a page is rendered, this makes ZUL access a lazy-loaded collection without errors '''when you visit the ZUL at first request'''. However, if your event handling methods (or command methods) accesses a lazy-loaded property of a detached object, you will still get a <tt>LazyInitializationException</tt> when a user interacts with the ZUL. This is because even though the filter opens a session for each request, the detached objects don't attach to the session automatically. <ref> The reason is explained in a [https://community.jboss.org/wiki/OpenSessionInView#Why_cant_Hibernate_just_load_objects_on_demand Hibernate article "Open Session in View"] </ref> The two DAO implementations demonstrated previously both have this problem and there are two solutions for it.
+
Although we apply ''open session in view'' pattern to keep a session open when a page is rendered, this makes ZUL access a lazy-loaded collection without errors '''when you visit the ZUL at first request'''. However, if your event handling methods (or command methods) accesses a lazy-loaded property of a detached object, you will still get a <code>LazyInitializationException</code> when a user interacts with the ZUL. This is because even though the filter opens a session for each request, the detached objects don't attach to the session automatically. <ref> The reason is explained in a [https://community.jboss.org/wiki/OpenSessionInView#Why_cant_Hibernate_just_load_objects_on_demand Hibernate article "Open Session in View"] </ref> The two DAO implementations demonstrated previously both have this problem and there are two solutions for it.
  
 
# set fetching strategy to '''eagerly fetch'''.
 
# set fetching strategy to '''eagerly fetch'''.
Line 263: Line 340:
 
We will use an example to demonstrate the second solution. The following is a "Order Viewer" system. The upper ''Listbox'' contains a list of orders and the lower ''Grid'' contains the details of items that are selected in the order. One order may contain many order items (one-to-many mapping), and we set order items collection to lazy fetching . When we select an order, the ''Grid'' displays detailed items of the selected order which means accessing a lazy-loaded collection.
 
We will use an example to demonstrate the second solution. The following is a "Order Viewer" system. The upper ''Listbox'' contains a list of orders and the lower ''Grid'' contains the details of items that are selected in the order. One order may contain many order items (one-to-many mapping), and we set order items collection to lazy fetching . When we select an order, the ''Grid'' displays detailed items of the selected order which means accessing a lazy-loaded collection.
  
<source lang="java" high="11,12">
+
<source lang="java" highlight="11,12">
 
@Entity
 
@Entity
 
@Table(name="orders")
 
@Table(name="orders")
Line 282: Line 359:
  
  
By default, we set the ''Listbox'' selection on the first row. When ZUL accesses the selected order's lazy-loaded items collection, Hibernate can load it successfully with the help of open-session-in-view filter because session is still open. However, if we click the second row which also accesses a detached <tt>Order</tt> object's items collection, we should re-load the object with Hibernate session or we'll get <tt>LazyInitializationException</tt>.  
+
By default, we set the ''Listbox'' selection on the first row. When ZUL accesses the selected order's lazy-loaded items collection, Hibernate can load it successfully with the help of open-session-in-view filter because session is still open. However, if we click the second row which also accesses a detached <code>Order</code> object's items collection, we should re-load the object with Hibernate session or we'll get <code>LazyInitializationException</code>.  
  
 
[[File:Hibernate-beginning.png | 500px | center]]
 
[[File:Hibernate-beginning.png | 500px | center]]
Line 291: Line 368:
  
 
'''Reload selected object'''
 
'''Reload selected object'''
<source lang="java" high="11, 16">
+
<source lang="java" highlight="11, 16">
  
 
public class OrderViewModel {
 
public class OrderViewModel {
Line 317: Line 394:
  
 
</source>
 
</source>
* Line 11: Initialize the ''Listbox'' selection with the <tt>Order</tt> object at index 0 of <tt>orders</tt>.
+
* Line 11: Initialize the ''Listbox'' selection with the <code>Order</code> object at index 0 of <code>orders</code>.
* Line 16: Re-query the <tt>selectedItem</tt>.
+
* Line 16: Re-query the <code>selectedItem</code>.
  
  
We use <tt>Session.load()</tt> to re-query the <tt>Order</tt> object with its id, this newly-retrieved object still has an open Hibernate session. Then when we access the lazy-loaded collection (<tt>items</tt>) in the ZUL, Hibernate can retrieve the collection for us. After doing so, we can eliminate <tt>LazyInitializationException</tt>.  
+
We use <code>Session.load()</code> to re-query the <code>Order</code> object with its id, this newly-retrieved object still has an open Hibernate session. Then when we access the lazy-loaded collection (<code>items</code>) in the ZUL, Hibernate can retrieve the collection for us. After doing so, we can eliminate <code>LazyInitializationException</code>.  
  
 
'''Re-attach to a session'''
 
'''Re-attach to a session'''
<source lang="java" high="11">
+
<source lang="java" highlight="11">
  
 
public class OrderDao {
 
public class OrderDao {
Line 343: Line 420:
 
= Lazy Initialization Issue Under Render on Demand =
 
= Lazy Initialization Issue Under Render on Demand =
  
Some AU requests cannot be interfered by developers,such as a "[[ZK Developer's Reference/Performance Tips/Listbox, Grid and Tree for Huge Data/Turn on Render on Demand|Render On Demand]]" request where the rendering request is handled implicitly by a component itself. Under this situation, if a component needs to '''render some data from a detached object's lazy-loaded collection''', developers won't have a chance to reload detached objects during the rendering to avoid <tt>LazyInitailizationException</tt>.
+
Some AU requests cannot be interfered by developers,such as a "[[ZK Developer's Reference/Performance Tips/Listbox, Grid and Tree for Huge Data/Turn on Render on Demand|Render On Demand]]" request where the rendering request is handled implicitly by a component itself. Under this situation, if a component needs to '''render some data from a detached object's lazy-loaded collection''', developers won't have a chance to reload detached objects during the rendering to avoid <code>LazyInitailizationException</code>.
  
Assume we have a ''Listbox'', it only displays 10 rows by default and it'a not in "page" mold. One of its columns display a lazy-loaded collection's size (<tt>each.items.size()</tt> )of an <tt>Order</tt> object.
+
Assume we have a ''Listbox'', it only displays 10 rows by default and it'a not in "page" mold. One of its columns display a lazy-loaded collection's size (<code>each.items.size()</code> )of an <code>Order</code> object.
  
 
'''Listbox that accesses lazy-loaded property'''
 
'''Listbox that accesses lazy-loaded property'''
<source lang="xml" high="4, 14">
+
<source lang="xml" highlight="4, 14">
  
 
<window title="" border="normal" width="600px" apply="org.zkoss.bind.BindComposer"
 
<window title="" border="normal" width="600px" apply="org.zkoss.bind.BindComposer"
Line 370: Line 447:
 
</source>
 
</source>
  
If we just pass a Java <tt>List</tt> object to be the model of the ''Listbox'', when a user scrolls down to view other rows, ZK will send AU request to retrieve data for those un-rendered rows. ''Listbox'' will try to access lazy-loaded collection but objects in the list are already detached, and we will get <tt>LazyInitailizationException</tt>. During this rendering process, developers will not be notified and cannot interfere this process to reload detached objects. One solution is to '''implement a custom <javadoc>org.zkoss.zul.ListModel</javadoc>''' for the component.
+
If we just pass a Java <code>List</code> object to be the model of the ''Listbox'', when a user scrolls down to view other rows, ZK will send AU request to retrieve data for those un-rendered rows. ''Listbox'' will try to access lazy-loaded collection but objects in the list are already detached, and we will get <code>LazyInitailizationException</code>. During this rendering process, developers will not be notified and cannot interfere this process to reload detached objects. One solution is to '''implement a custom <javadoc>org.zkoss.zul.ListModel</javadoc>''' for the component.
  
 
We demonstrate 2 implementations here for your reference. The first one is simpler but less efficient; it re-queries each detached object when a component requests it.
 
We demonstrate 2 implementations here for your reference. The first one is simpler but less efficient; it re-queries each detached object when a component requests it.
  
 
'''Reloaded ListModel'''
 
'''Reloaded ListModel'''
<source lang="java" high="1, 6,14">
+
<source lang="java" highlight="1, 6,14">
 
public class OrderListModel extends AbstractListModel<Order>{
 
public class OrderListModel extends AbstractListModel<Order>{
  
Line 399: Line 476:
 
}
 
}
 
</source>
 
</source>
* Line 1: We extends <javadoc>org.zkoss.zul.AbstractListModel</javadoc> to build our list model for it handles selection for us, but we have to implement Order's <tt>equals()</tt> and <tt>hashCode()</tt>.
+
* Line 1: We extends <javadoc>org.zkoss.zul.AbstractListModel</javadoc> to build our list model for it handles selection for us, but we have to implement Order's <code>equals()</code> and <code>hashCode()</code>.
 
* Line 14: Re-query the detached object by its id and return a persistent one.
 
* Line 14: Re-query the detached object by its id and return a persistent one.
  
Line 406: Line 483:
  
 
'''Lived ListModel'''
 
'''Lived ListModel'''
<source lang="java" high="16, 42">
+
<source lang="java" highlight="16, 42">
 
public class LiveOrderListModel extends AbstractListModel<Order>{
 
public class LiveOrderListModel extends AbstractListModel<Order>{
  
Line 469: Line 546:
 
}
 
}
 
</source>
 
</source>
* Line 16: If the cache doesn't contain target <tt>Order</tt>, we query a one page size of <tt>Order</tt> starting from the index as passed <tt>index</tt> doesn't always increase sequentially.
+
* Line 16: If the cache doesn't contain target <code>Order</code>, we query a one page size of <code>Order</code> starting from the index as passed <code>index</code> doesn't always increase sequentially.
* Line 42: The <tt>getElementAt(int)</tt> will be invoked multiple times during an execution. In order to avoid using a cache of detached objects, we make the cache as an attribute of an execution which is dropped after being handled.
+
* Line 42: The <code>getElementAt(int)</code> will be invoked multiple times during an execution. In order to avoid using a cache of detached objects, we make the cache as an attribute of an execution which is dropped after being handled.
  
 
= Get Example Source Code =  
 
= Get Example Source Code =  
All source code used in this chapter can be found [https://code.google.com/p/zkbooks/source/browse/#svn%2Ftrunk%2Fdevelopersreference%2Fintegration.hibernate here].
+
All source code used in this chapter can be found [https://github.com/zkoss/zkbooks/tree/master/developersreference/integration.hibernate here].
  
 
= Reference =  
 
= Reference =  
 
<references/>
 
<references/>
  
=Version History=
+
 
{{LastUpdated}}
 
{| border='1px' | width="100%"
 
! Version !! Date !! Content
 
|-
 
| 6.5.0
 
| November 2012
 
| Rewrite for improvement.
 
|}
 
  
 
{{ZKDevelopersReferencePageFooter}}
 
{{ZKDevelopersReferencePageFooter}}

Latest revision as of 04:34, 5 February 2024


Overview

Due to object/relational paradigm mismatch [1], developers tend to use ORM (object/relational mapping) framework to convert object-oriented model to relational model and vice versa. Hibernate is the most popular ORM framework in Java world. We will talk about some integration topics in this chapter such as lazy initialization with Spring. If you haven't read about basic concepts and installation of Hibernate, please refer to Hibernate Dcumentation. The example we give in this chapter is based on Hibernate 4.0.0.final and Spring 3.1.2.RELEASE.

Integrate with Different DAO implementation

The Data Access Object (DAO) pattern is a good practice to implement a persistence layer. This pattern encapsulates data access codes written by Hibernate API from business tier. A DAO object exposes an interface to business object and performs persistence operation relating to a particular persistent entity.

According to Hibernate Reference Manual's suggestion [2], we should apply session-per-request pattern to manage sessions and transactions. This pattern needs an interceptor to open a contextual session with a transaction when a request is going to be handled and close the session before respond is sent to client (aka. open session in view pattern). A common implementation for page-based application is a servlet filter.

Applying this pattern also solves a common "LazyInitializationException" problem that most developers encounter when using lazy fetching strategy. In brief, Hibernate session is usually closed after a DAO object has performed an operation (a.k.a session-per-operation pattern). Those persistent objects become detached after the associated sessions are closed. If we access a detached object's lazy-loaded collection when rendering the view. We will get an error message like LazyInitializationException: no session or session was closed. For detailed explanation, please refer to the article "Open Session in View" on Hibernate community[3]. As we apply session-per-request pattern, a Hibernate session is kept open when a View is accessing lazy-loaded collection. Those objects queried by the Hibernate session becomes detached later (after the interceptor closes the Hibernate session), so the previously mentioned problem is resolved.


Homemade DAO

Here we introduce how to implement an DAO without other frameworks (e.g. Spring).

Configuration

The minimal Maven dependency you need is:

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>4.0.0.Final</version>
      <scope>compile</scope>
    </dependency>

Icon info.png Note: If you don't use Maven, please refer to Hibernate Reference Documentation to know which JAR file you need.


Utility Class

A simple way to implement a DAO is to control Hibernate sessions and transactions manually, hence we need a utility class to get SessionFactory. ZK's HibernateUtil has been deprecated since 6.0.2, you can write your own one according to the code example in Hibernate Reference Manual.[4] Here we provide a simple example.

Utility class to get SessionFactory

package org.zkoss.reference.developer.hibernate.dao;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static SessionFactory sessionFactory;
    static {
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

An Open Session Listener

For open session in view pattern, we need an interceptor to open sessions. In ZK, we need to intercept all requests including AU requests, so we implement ZK's Life Cycle Listener to achieve this. (Our listener's implementation is based on the filter mentioned by a Hibernate article "Open Session in View". [5].) This listener opens a session and begins a transaction at the beginning of an execution (ZK's HTTP request wrapper, then commits (or rollback) at the end of the execution.

Extracted from OpenSessionInViewListener

public class OpenSessionInViewListener implements ExecutionInit, ExecutionCleanup {
	private static final Log log = Log.lookup(OpenSessionInViewListener.class);

	public void init(Execution exec, Execution parent) {
		if (parent == null) { //the root execution of a servlet request
			log.debug("Starting a database transaction: "+exec);
			HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
		}
	}

	public void cleanup(Execution exec, Execution parent, List errs) {
		if (parent == null) { //the root execution of a servlet request
			if (errs == null || errs.isEmpty()) {
				log.debug("Committing the database transaction: "+exec);
				HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
			} else {
				final Throwable ex = (Throwable) errs.get(0);
				rollback(exec, ex);
			}
		}
	}

	private void rollback(Execution exec, Throwable ex) {
		try {
			if (HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().isActive()) {
				log.debug("Trying to rollback database transaction after exception:"+ex);
				HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
			}
		} catch (Throwable rbEx) {
			log.error("Could not rollback transaction after exception! Original Exception:\n"+ex, rbEx);
		}
	}
}
  • Line 7: Call getCurrentSession() to get a contextual session. [6]

Add configuration in zk.xml to make the listener work.

Configure listener in zk.xml

<zk>
	<listener>
		<listener-class>org.zkoss.reference.developer.hibernate.web.OpenSessionInViewListener</listener-class>
	</listener>
</zk>

DAO Implementation

The listener begins and commits transactions keeping DAO's implementation simple. Just use the utility class to get current Hibernate session to perform the operation.

Simple DAO implementation

public class OrderDao {

	public List<Order> findAll() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Query query = session.createQuery("select o from Order as o");
		List<Order> result = query.list();
		return result;
	}

	/**
	 * rollback is handled in filter.
	 * @param newOrder
	 * @return
	 * @throws HibernateException
	 */
	public Order save(Order newOrder) throws HibernateException{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.save(newOrder);
		session.flush();
		return newOrder;
	}
}

Finally, we can use the DAO class in a ViewModel to manipulate domain objects.

Use DAO in a ViewModel

public class OrderViewModel {

	private OrderDao orderDao = new OrderDao();

	private List<Order> orders ;
	private Order selectedItem;
	
	@Init
	public void init(){
		orders = orderDao.findAll();
		if (!orders.isEmpty()){
			setSelectedItem(orders.get(0));
		}
	}

	//omit setter and getter for brevity
}

Spring-based DAO

With Spring provided dependency injection and ORM support, here our efforts are simplified quite substantially. We'll demonstrate one typical usage in non-managed environment. To apply session-per-request pattern, we can use Spring provided OpenSessionInViewFilter instead of writing our own one. According to the suggestion in Spring Reference Documentation 3.1, using plain Hibernate API to implement a DAO is the current recommended usage pattern. In the DAO, we can also easily retrieve SessionFactory by dependency inject without any utility classes (HibernateUtil). Besides, declarative transaction management and rollback rule also reduces our work. The following are the the related code snippets.

Configuration

The minimal dependencies you need are Hibernate-Core, Spring-Web, Spring-ORM, and Cglib:

  	<dependency>
  		<groupId>org.hibernate</groupId>
  		<artifactId>hibernate-core</artifactId>
  		<version>4.0.0.Final</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-web</artifactId>
  		<version>3.1.2.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-orm</artifactId>
  		<version>3.1.2.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>cglib</groupId>
  		<artifactId>cglib</artifactId>
  		<version>2.2</version>
  	</dependency>

We use basic configuration for Spring.

Spring configuration for Hibernate

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/tx
		   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
		<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
		<property name="url" value="jdbc:hsqldb:file:data/store" />
		<property name="username" value="sa" />
		<property name="password" value="" />
	</bean>
	<!-- 
		hibernate.current_session_context_class=thread
 	-->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.HSQLDialect
				hibernate.hbm2ddl.auto=crate
				hibernate.show_sql=true
				hibernate.connection.pool_size=5
				hibernate.connection.autocommit=false
			</value>
		</property>
		<property name="annotatedClasses">
			<list>
				<value>org.zkoss.reference.developer.hibernate.domain.Order</value>
				<value>org.zkoss.reference.developer.hibernate.domain.OrderItem</value>
			</list>
		</property>
	</bean>
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<tx:annotation-driven />
	
	<!-- Scans for application @Components to deploy -->
	<context:component-scan base-package="org.zkoss.reference.developer.hibernate" />
</beans>
  • Line 40: For Hibernate 3.x, some package names should be changed to org.springframework.orm.hibernate3.*, e.g. org.springframework.orm.hibernate3.HibernateTransactionManager.

OpenSessionInViewFilter

Spring already provides a OpenSessionInViewFilter to solve lazy loading in web views problems. This filter makes Hibernate Sessions available via the current thread, which will be auto-detected by Spring's transaction managers. For detailed description and usage, please refer to Spring's Javadoc.

Configure OpenSessionInViewFilter in web.xml

	<filter>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


DAO Implementation

For a Spring-powered DAO, we can use injected SessionFactory and @Transactional to perform persistence operation.

DAO empowered by Spring

@Repository
public class SpringOrderDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Transactional(readOnly=true)
	public List<Order> queryAll() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select o from Order as o");
		List<Order> result = query.list();
		return result;
	}

	@Transactional
	public Order save(Order newOrder){
		Session session = sessionFactory.getCurrentSession();
		session.save(newOrder);
		session.flush();
		return newOrder;
	}

	//omit other codes
}


To use this Spring-based DAO in a composer (or a ViewModel), ZK provides several ways like variable resolvers. Please refer to ZK Developer's Reference/Integration/Middleware Layer/Spring.


Lazy Initialization Issue among AU Requests

Although we apply open session in view pattern to keep a session open when a page is rendered, this makes ZUL access a lazy-loaded collection without errors when you visit the ZUL at first request. However, if your event handling methods (or command methods) accesses a lazy-loaded property of a detached object, you will still get a LazyInitializationException when a user interacts with the ZUL. This is because even though the filter opens a session for each request, the detached objects don't attach to the session automatically. [7] The two DAO implementations demonstrated previously both have this problem and there are two solutions for it.

  1. set fetching strategy to eagerly fetch.
  2. Re-query the detached object manually.

If you are not dealing with large amount of data, you can choose the first solution by simply changing your fetching strategy to eager for properties.

We will use an example to demonstrate the second solution. The following is a "Order Viewer" system. The upper Listbox contains a list of orders and the lower Grid contains the details of items that are selected in the order. One order may contain many order items (one-to-many mapping), and we set order items collection to lazy fetching . When we select an order, the Grid displays detailed items of the selected order which means accessing a lazy-loaded collection.

@Entity
@Table(name="orders")
public class Order {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	private String status = PROCESSING;
	private String description;

	@OneToMany(mappedBy="orderId", fetch=FetchType.LAZY)
	private List<OrderItem> items = new ArrayList<OrderItem>();

	//other codes...
}


By default, we set the Listbox selection on the first row. When ZUL accesses the selected order's lazy-loaded items collection, Hibernate can load it successfully with the help of open-session-in-view filter because session is still open. However, if we click the second row which also accesses a detached Order object's items collection, we should re-load the object with Hibernate session or we'll get LazyInitializationException.

Hibernate-beginning.png

In order to reload a detached object, we pass the selected order to DAO object and reload it.

Reload selected object

public class OrderViewModel {

	private OrderDao orderDao = new OrderDao();

	private List<Order> orders ;
	private Order selectedItem;
	
	@Init
	public void init(){
		orders = orderDao.findAll();
		setSelectedItem(orders.get(0));
	}

	public Order getSelectedItem() {
		if (selectedItem!=null){
			selectedItem = orderDao.reload(selectedItem);
		}
		return selectedItem;
	}

	//omit other methods for brevity
}
  • Line 11: Initialize the Listbox selection with the Order object at index 0 of orders.
  • Line 16: Re-query the selectedItem.


We use Session.load() to re-query the Order object with its id, this newly-retrieved object still has an open Hibernate session. Then when we access the lazy-loaded collection (items) in the ZUL, Hibernate can retrieve the collection for us. After doing so, we can eliminate LazyInitializationException.

Re-attach to a session

public class OrderDao {

	//...

	/**
	 * Initialize lazy-loaded collection.
	 * @param order
	 * @return
	 */
	public Order reload(Order order){
		return (Order)HibernateUtil.getSessionFactory().getCurrentSession().load(Order.class,order.getId());
	}
}

Lazy Initialization Issue Under Render on Demand

Some AU requests cannot be interfered by developers,such as a "Render On Demand" request where the rendering request is handled implicitly by a component itself. Under this situation, if a component needs to render some data from a detached object's lazy-loaded collection, developers won't have a chance to reload detached objects during the rendering to avoid LazyInitailizationException.

Assume we have a Listbox, it only displays 10 rows by default and it'a not in "page" mold. One of its columns display a lazy-loaded collection's size (each.items.size() )of an Order object.

Listbox that accesses lazy-loaded property

	<window title="" border="normal" width="600px" apply="org.zkoss.bind.BindComposer"
		viewModel="@id('vm') @init('org.zkoss.reference.developer.hibernate.vm.RodViewModel')">
		Contain a customized model that reload lazy-loaded collection from database
		<listbox model="@load(vm.orderListModel)" rows="10">
			<listhead>
				<listheader label="ID" width="50px" />
				<listheader label="Description" />
				<listheader label="Item Count" width="150px" />
			</listhead>
			<template name="model">
				<listitem>
					<listcell label="@load(each.id) " />
					<listcell label="@load(each.description)" />
					<listcell label="@load(each.items.size())" />
				</listitem>
			</template>
		</listbox>
...

If we just pass a Java List object to be the model of the Listbox, when a user scrolls down to view other rows, ZK will send AU request to retrieve data for those un-rendered rows. Listbox will try to access lazy-loaded collection but objects in the list are already detached, and we will get LazyInitailizationException. During this rendering process, developers will not be notified and cannot interfere this process to reload detached objects. One solution is to implement a custom ListModel for the component.

We demonstrate 2 implementations here for your reference. The first one is simpler but less efficient; it re-queries each detached object when a component requests it.

Reloaded ListModel

public class OrderListModel extends AbstractListModel<Order>{

	private OrderDao orderDao;
	List<Order> orderList = new LinkedList<Order>();
	
	public OrderListModel(List<Order> orders,OrderDao orderDao){
		this.orderList = orders;
		this.orderDao = orderDao;
	}
	
	@Override
	public Order getElementAt(int index) {
		//throw a runtime exception if orderDao does not find target object
		Order renewOrder = orderDao.reload(orderList.get(index));
		return renewOrder;
	}

	@Override
	public int getSize() {
		return orderList.size();
	}
}
  • Line 1: We extends AbstractListModel to build our list model for it handles selection for us, but we have to implement Order's equals() and hashCode().
  • Line 14: Re-query the detached object by its id and return a persistent one.


The second one is more complicated but more efficient; it re-queries a one page size data each time and stores as a cache in an execution. If the cache has the object that the component requests, it returns the one in cache without re-querying again.

Lived ListModel

public class LiveOrderListModel extends AbstractListModel<Order>{

	private OrderDao orderDao;
	private Integer totalSize;	
	private int pageSize = 30;
	private final String CACHE_KEY= LiveOrderListModel.class+"_cache";
	
	public LiveOrderListModel(OrderDao orderDao){
		this.orderDao = orderDao;
	}

	/**
	 * query one page size of entity for one execution a time. 
	 */
	@Override
	public Order getElementAt(int index) {
		Map<Integer, Order> cache = getCache();

		Order targetOrder = cache.get(index);
		if (targetOrder == null){
			//if cache doesn't contain target object, query a page starting from the index
			List<Order> pageResult = orderDao.findAll(index, pageSize);
			int indexKey = index;
			for (Order o : pageResult ){
				cache.put(indexKey, o);
				indexKey++;
			}
		}else{
			return targetOrder;
		}

		//get the target after query from database
		targetOrder = cache.get(index);
		if (targetOrder == null){
			//if we still cannot find the target object from database, there is inconsistency in the database
			throw new HibernateException("Element at index "+index+" cannot be found in the database.");
		}else{
			return targetOrder;
		}
	}

	private Map<Integer, Order> getCache(){
		Execution execution = Executions.getCurrent();
		//we only reuse this cache in one execution to avoid accessing detached objects.
		//our filter opens a session during a HTTP request
		Map<Integer, Order> cache = (Map)execution.getAttribute(CACHE_KEY);
		if (cache == null){
			cache = new HashMap<Integer, Order>();
			execution.setAttribute(CACHE_KEY, cache);
		}
		return cache;
	}
	
	@Override
	public int getSize() {
		if (totalSize == null){
			totalSize = orderDao.findAllSize().intValue();
		}
		return totalSize; 
	}
}
  • Line 16: If the cache doesn't contain target Order, we query a one page size of Order starting from the index as passed index doesn't always increase sequentially.
  • Line 42: The getElementAt(int) will be invoked multiple times during an execution. In order to avoid using a cache of detached objects, we make the cache as an attribute of an execution which is dropped after being handled.

Get Example Source Code

All source code used in this chapter can be found here.

Reference




Last Update : 2024/02/05

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