0

can I use getHibernateTemplate() with org.zkoss.zkplus.hibernate.HibernateSessionContextListener?

asked 2009-11-26 20:59:18 +0800

hawk gravatar image hawk
3250 1 5
http://hawkphoenix.blogsp... ZK Team

updated 2009-11-27 00:58:03 +0800

Hi pals here,
I encounter lazy initialization problem of Hibernate.
I have set zk.xml according to small talk "ZK + Hibernate", http://www.zkoss.org/smalltalks/hibernatezk/hibernatezk.dsp

zk.xml
<!-- Configure the Hibernate SessionFactory Lifecycle. -->
<listener>
<description>Hibernate SessionFactory Lifecycle</description>
<listener-class>org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener</listener-class>
</listener>


<!-- Configure the Hibernate "Open Session In View" Session Lifecycle -->
<listener>
<description>Hibernate "Open Session In View" Session Lifecycle</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>

hibernate.cfg.xml
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">hr</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hr</property>
<property name="hibernate.connection.username">hr</property>
<property name="hibernate.default_schema">hr</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Bind the getCurrentSession() method to the thread. -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- Hibernate's transaction factory -->
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

My all DAO class all extends Spring's HibernateDaoSupport.

public abstract class AbstractDAO extends HibernateDaoSupport{
public void saveOrUpdate(Object obj) {

getHibernateTemplate().saveOrUpdate(obj);
}

public void delete(Object obj) {
getHibernateTemplate().delete(obj);
}
}

I have tried, if I use getHibernateTemplate() to query, I will get "lazy initialization.. session closed" problem.
But, if I use getSession().createQuery() to query, it works normally.

so, it means I can't use getHibernateTemplate() with ZK?
what's the reason?

Thanks for help.

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2009-11-27 07:08:46 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2009-11-27 07:09:47 +0800

You can download a bigger sample app here.
The backend is completely independent from zkoss and uses getHibernateTemplate() in the DAOs.
ZKoss calls the ServiceMethods --> calls DAO methods

best
Stephan

link publish delete flag offensive edit

answered 2009-11-30 04:10:56 +0800

hawk gravatar image hawk
3250 1 5
http://hawkphoenix.blogsp... ZK Team

Hi, Stephan:
I have downloaded your sample project frontend and backend.
But in zk_sample_gui, web.xml, zk.xml, I don't see you using any listener or filter to solve Hibernate's lazy initialization problem.
so, can you briefly explain how you solve this issue?

and how to deploy your backend project, I only saw lots of jar in lib folder.

link publish delete flag offensive edit

answered 2009-11-30 12:01:18 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2009-11-30 12:25:33 +0800

Edit: Argh. I lost the text by saving.
Now smaller, sorry.

Hi hawk,

1. the sample app depends on TWO eclipse projects. You must download both (frontend and backend).

2. If you have both projects, only start the zk_sample_gui with run as 'Run on Server'

3. When Tomcat is starting a inMemory H2 DB is started and creates the tables and filled with demo data from a script automatically.

4. I'm not working with OpenSessionInViewListener. I load the releated objects early. For lists i use paging and the
Search Class from the Hibernate-Generic-DAO framework with Search.addFetch("myRelatedTable"). Related objects are in this case
loaded early too. It's a philosopie what you take. In my case i'm thinking all times for a bigger multi-user case. So i don't can accept that the
session holds the tables for a long or uncontrolled time open.

5. The sources are complete but not upToDate. Next Release on we are working since 2 weeks have much changes depends on spring.
Zk's multi-threading architecture is not build out of the box to work easily with spring.


best
Stephan

PS: |how to deploy your backend project...

Nov 03, 2009

Yes it's like obiwan says.
The frontend project have a dependeny to the backend project.
You can see the settings for this in eclipse.

For a normal swing application you can add needed sub-project to your main project by adding these in the project properties
Category 'Build Path'

In a web project you would bundle all sub-projects(jars) in the same war-file for exporting. So you must done these by adding
these sub-jars as EE module dependencies.

-> (right click on zk_sample_gui --> properties -->JAVA EE Module Dependency

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-11-26 20:59:18 +0800

Seen: 1,148 times

Last updated: Nov 30 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More