0

Problems with ZK-Spring-Hibernate

asked 2007-06-20 16:34:01 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: matteo_barbieri

Hi,
sometimes, when I try to save, update or delete object (so write to database) I got the famous exception:

"org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions"

Using some tricks I can usually find a workaround, but I would like to ask you if there is a more elegant solution.
I'm not a Spring/Hibernate expert, maybe that's why I have those problems...
I try to give you as much information as I can, please tell me if you need
more:

in zk.xml:
------------
<listener>
<description>Spring TransactionSynchronizationManager handler</description>
<listener-class>org.zkoss.zkplus.spring.SpringTransactionSynchronizationListen
er</listener-class>
</listener>
------------

in web.xml
------------
<!-- Hibernate OpenSession Filter -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.zul</url-pattern>
</filter-mapping>
------------

in applicationContext.xml
------------
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
....
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDB
CTransactionFactory</prop>
</props>
</property>
</bean>

<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --> <bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>

<!-- DAOs -->
<bean id="myClassDao" class="com.aaa.dao.impl.MyClassHibernateDAO">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
<property name="persistentClass">
<value>com.aaa.hib.MyClass</value>
</property>
</bean>
------------

Then I have some interfaces, here I put only implementation classes.

There is an abstract GenericDAO, all DAOs extends this class.

in GenericHibernateDAO.java:
-------------
public abstract class GenericHibernateDAO<T, ID extends Serializable>
implements GenericDAO<T, ID>, InitializingBean {

private Class<T> persistentClass;

private SessionFactory sessionFactory;

public GenericHibernateDAO() {}

public GenericHibernateDAO(SessionFactory sessionFactory,
Class<T> persistentClass) {
this.persistentClass = persistentClass;
this.sessionFactory = sessionFactory;
}

protected Session getSession() {
return SessionFactoryUtils.getSession(sessionFactory, true);
}
.... getters and setters and other methods ....

public void saveOrUpdate(T entity) {
getSession().saveOrUpdate(entity);
}
}
------------

Sorry for the long post... maybe the solution is simple (I hope so..) Thanks everybody,

Bye




delete flag offensive retag edit

15 Replies

Sort by ยป oldest newest

answered 2007-06-20 17:36:16 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: sousa1981

Take a look at http://en.wikibooks.org/wiki/ZK/How-Tos there is an entry ZK 2.x.x + Spring 2.x.x + Hibernate 3.x + JUnit Test

Regards,

Marcos de Sousa

link publish delete flag offensive edit

answered 2007-06-21 00:31:50 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: henrichen

<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.zul</url-pattern>
</filter-mapping>

This might not be enough. It handles the "loading" of the zul page only. It does not cover Ajax parts that is /appName/zkau/* when an user for example click a button.

I usually user /appName/* to guarantee "any" request for this application will go thru the filter though a bit of "wasting" CPU time.

/henri

link publish delete flag offensive edit

answered 2007-06-21 07:03:41 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: matteo_barbieri

Thank you,
I put /appName/* but this doesn't solve the problem.
Bye

link publish delete flag offensive edit

answered 2007-06-21 08:54:18 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: sousa1981

Henri,

In http://en.wikibooks.org/wiki/ZK/How-Tos there is an entry ZK 2.x.x + Spring 2.x.x + Hibernate 3.x + JUnit Test, and I am using:

<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

But according your message it will not cover every situation.
and I really know that "It does not cover Ajax parts that is /appName/zkau/* when an user for example click a button."

How can be modified the web.xml to covery every thing?

Because, I tried with NO SUCCESS:
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/WikiBook/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/appName/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.zhtml</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/zk/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/zkau</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/zkau/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.zul</url-pattern>
</filter-mapping>

If I in simple ZUL file it work well, but on click a button not work well.

Thanks in advance.

Regards,

Marcos de Sousa

link publish delete flag offensive edit

answered 2007-07-02 10:18:57 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: matteo_barbieri

@Marcos I tried your solution, but is really necessary to have 2 classes (manager and dao) for each class?

I'm still having this problem, any other idea?
Thank you,
bye

link publish delete flag offensive edit

answered 2007-07-02 11:05:18 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: sousa1981

NO.

Those showed is recommended solution, but u could skip it.

Regards,

Marcos de Sousa

link publish delete flag offensive edit

answered 2007-07-03 08:23:47 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: henrichen

There would be at least two kinds of request.

1. When loading a page. say *.zul, *.zhtml

2. When sending Ajax request. It is /zkau/*.

So /* should cover everything.

My previous /appName/zkau/* is not correct since web.xml should handle the appName part automatically. Sorry for that.

/henri

link publish delete flag offensive edit

answered 2007-07-03 08:53:57 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: matteo_barbieri

If I put /* or /zkau/* in web.xml I got this exception when I load an object with relationship:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role

Setting *.zul I got no problem when loading, but when saving/updating.

P.S. I had to set
<param-name>singleSession</param-name>
<param-value>true</param-value>
I was wrong when copy/pasted the code.

I also tried Marcos' solution, but same problem.


link publish delete flag offensive edit

answered 2007-07-03 08:55:33 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: matteo_barbieri

I also tried using getHibernateTemplate() in my GenericDAO, nothing..

link publish delete flag offensive edit

answered 2007-07-03 09:26:14 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: matteo_barbieri

More debug:
I activated debug logs on the OpenSessionInViewFilter to see when the filter is applied, and you were right, with /* it's applied at every request, while with *.zul only on page load.
But now I've a different problem, I got the "LazyInitializationException:19
- failed to lazily initialize a collection of role" exception, even when loading an object with some relationship.

11:15:40,128 DEBUG OpenSessionInViewFilter:239 - Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter 11:15:40,130 DEBUG OpenSessionInViewFilter:181 - Opening single Hibernate Session in OpenSessionInViewFilter
11:15:40,178 ERROR LazyInitializationException:19 - failed to lazily initialize a collection of role: mypackage.MyClass, no session or session was closed

Bye




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: 2007-06-20 16:34:01 +0800

Seen: 509 times

Last updated: Jul 05 '07

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