0

Problem with Hibernate

asked 2006-11-27 15:52:32 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4032408

By: taffit_

Hello @all,

I have a problem with Hibernate and the Hibernate-session. I've read everything here in the forum, the documentation and the examples and tried for a few days different settings, but with no results.
It seems that the session is not set by the constructor, although when I get it within the method, everything works fine.
My setup:
- Tomcat 5.5 on Apache 2 on Solaris
- Hibernate 3
- zk tried with 2.1 and 2.2 RC

I have all needed libs in the WEB-INF\lib (Hibernate, zk, jdbc-driver).

Here are my files (simplified):

zk.xml:
<zk>
<listener>
<description>Hibernate SessionFactory lifecycle</description>
<listener-class>org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener</li
stener-class>
</listener>
<listener>
<description>Hibernate Open Session In View lifecycle</description>
<listener-class>org.zkoss.zkplus.hibernate.OpenSessionInViewListener</listener
-class>
</listener>
<listener>
<description>Hibernate thread session context handler</description>
<listener-class>org.zkoss.zkplus.hibernate.HibernateSessionContextListener</li
stener-class>
</listener>
</zk>

index.zul:
<zk>
<window id="wMain" use="eventGUI.EventGUI" onCreate="wMain.EventGUI()">
<groupbox id="gbFilter" mold="3d" width="100%">
<caption label="Filter" id="cFilter">
<button id="btnTest" label="Test" onClick="wMain.getEntries()" />
</caption>
</groupbox>
</window>
</zk>

WEB-INF/classes/hibernate.cfg.xml:
<?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>
<!-- local connection properties -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/adb</pro
perty>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop
erty>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">mypass</property>
<!--property name="hibernate.connection.pool_size">1</property-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<!-- Hibernate's transaction factory -->
<property
name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactor
y</property>
<!-- for using Hibernate within zk -->
<property name="current_session_context_class">thread</property>
<!-- Disable 2nd level cache -->
<property name="cache.use_second_level_cache">false</property>
<mapping resource="eventGUI/Operation.hbm.xml" />
</session-factory>
</hibernate-configuration>

WEB-INF/classes/eventGUI/Operation.hbm.xml and Operation.class containing the mappings

WEB-INF/classes/eventGUI/EventGUI.class:
package eventGUI;
import org.zkoss.zk.ui.*;
import org.zkoss.zul.*;
import eventGUI.EventDB;
import eventGUI.Operation;
import java.util.*;
public class EventGUI extends Window {
public void getEntries() throws Exception {
System.out.println("Executing getEntries().");
try {

List l = new EventDB().findAll();
if (l.isEmpty()) {
Messagebox.show("No entries!");
} else {
Messagebox.show("Entries found.");
}
} catch (Exception e) {
Messagebox.show("Exception: " + e.toString());
}
}
}

WEB-INF/classes/eventGUI/EventDB.class:
package eventGUI;
import org.zkoss.zkplus.hibernate.*;
import eventGUI.Operation;
import java.util.*;
import org.hibernate.*;
public class EventDB {
org.hibernate.Session _session;

public void EventDB() {
_session = HibernateUtil.getSessionFactory().getCurrentSession();
}

public List findAll() throws HibernateException {
// It only works if I uncomment the line below:
//_session = HibernateUtil.getSessionFactory().getCurrentSession();
return _session.createQuery("from Operation").list();
}
}

The problem is, that if I run it with the files above and click on the Test-button (calling the eventGUI.EventGUI.getEntries-method which in turn calls the eventGUI.
EventDB-constructor and its findAll-method) I get a NullPointerException on
eventGUI.EventDB.findAll() if it tries to access the _session-member variable.
If I try to get the _session in the findAll-method everything works as expected.

Any hint what I'm making wrong? In the essence this is the same as the example in the smalltalk about zk-Hibernate-session-listener, I just don't run the constructor and findAll-method in the zul-file but call a method of a class instead.

Any hints are greatly appreciated...

Thx in advance, taffit

delete flag offensive retag edit

5 Replies

Sort by ยป oldest newest

answered 2006-11-29 06:57:22 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4035390

By: henrichen

That is strange. Can you post the NullPointerException stack trace of the Tomcat's log?

Note that you don't do things like this:

EventDB edb = new EventDB();

...
Messagebox.show(...);


edb.findAll();


The modal window (The Messagebox) will stop the current thread and the http request completes. The session stored in (new EventDB()) has been closed, you have to getCurrentSession() again.

/henri


link publish delete flag offensive edit

answered 2006-11-29 11:59:26 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4035753

By: taffit_

Thanks for your reply, henrichen!

It seems that this was the problem (I had an additional messagebox in the getEntries-class).
So after I do something with zk (like opening a messagebox, updating a listbox
etc.) I have to get the session again?


Another question regarding Hibernate: it seems to only work with version 2.1 of the zk. When I try it with the 2.2 (RC or FL) I first get the following error (on startup of Tomcat):
29.11.2006 12:32:44 org.zkoss.zkplus.hibernate.HibernateUtil
initSessionFactory:83
SCHWERWIEGEND: Initial SessionFactory creation failed.java.lang.NoClassDefFoundError:
org/hibernate/cfg/AnnotationConfiguration
29.11.2006 12:32:44 org.zkoss.zk.ui.util.Configuration invokeWebAppInits:484
SCHWERWIEGEND: Failed to invoke class
org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener
java.lang.ExceptionInInitializerError
at org.zkoss.zkplus.hibernate.HibernateUtil.initSessionFactory(HibernateUtil.ja
va:84)
at org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener.init(HibernateSes
sionFactoryListener.java:46)
at org.zkoss.zk.ui.util.Configuration.invokeWebAppInits(Configuration.java:482)
at org.zkoss.zk.ui.http.WebManager.<init>(WebManager.java:168)
at org.zkoss.zk.ui.http.DHtmlLayoutServlet.init(DHtmlLayoutServlet.java:91)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:11
05)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:932)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:
3917)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4201)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:7
59)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:904)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:867
)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:474)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1122)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:310)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSuppor
t.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1021)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:709)
at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)
Caused by: java.lang.NoClassDefFoundError:
org/hibernate/cfg/AnnotationConfiguration
at org.zkoss.zkplus.hibernate.HibernateUtil.initSessionFactory(HibernateUtil.ja
va:78)
------------------------------

and if I copy the Hibernate-annotations-jar to the lib-directory of the app I get the following:
29.11.2006 12:35:45 org.zkoss.zkplus.hibernate.HibernateUtil
initSessionFactory:83
SCHWERWIEGEND: Initial SessionFactory creation failed.java.lang.IllegalAccessError:
tried to access field org.hibernate.cfg.Configuration.xmlHelper from class org.hibernate.cfg.AnnotationConfiguration
29.11.2006 12:35:45 org.zkoss.zk.ui.util.Configuration invokeWebAppInits:484
SCHWERWIEGEND: Failed to invoke class
org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener
java.lang.ExceptionInInitializerError
at org.zkoss.zkplus.hibernate.HibernateUtil.initSessionFactory(HibernateUtil.ja
va:84)
at org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener.init(HibernateSes
sionFactoryListener.java:46)
at org.zkoss.zk.ui.util.Configuration.invokeWebAppInits(Configuration.java:482)
at org.zkoss.zk.ui.http.WebManager.<init>(WebManager.java:168)
--------------------------------------

Has the Hibernate-util-package changed from 2.1 to 2.2? When I copy the zkplus.jar from 2.1 in the lib-directory, everything works fine...

link publish delete flag offensive edit

answered 2006-11-29 12:14:32 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4035770

By: henrichen

You have to copy ejb3-persistence.jar, Hibernate3.jar, and Hibernate-annotations.jar version 3.2.0.ga to WEB-INF/lib.

The ZK 2.2.0 version changed to supports Hibernate annotations.

/henri

link publish delete flag offensive edit

answered 2006-11-29 12:19:42 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4035778

By: henrichen

The Hibernate session is open and closed automatically by the OpenSessionInViewListner automatically. This is the so called session-per-request.

Each ZK event is an Ajax request, so the lifecycle of a hibernate session. Whenever an event is handled and response, a request is completed and a hibernate session is opened and closed.

The most safe way is to always get the current session by the method and don't cache it in any reference variable.

/henri

link publish delete flag offensive edit

answered 2006-11-29 15:38:20 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4036107

By: taffit_

Many many thanks henrichen for this detailed answer, it helps me (java AND zk
rookie) a lot...

All works like a charm now (well, maybe till I implement additional features ;-))...

Thanks again, taffit_

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: 2006-11-27 15:52:32 +0800

Seen: 611 times

Last updated: Nov 29 '06

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