0

Problem: listbox, livedata,spring-hibernate

asked 2007-04-04 10:34:23 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: grzegorzk__

Assume we have two domain classes: Resource and CmsType and Resource contains reference to CmsType:

---
Resource res = new Resource();
CmsType type = new CmsType();
res.setCmsType(type);
---

res.cmsType is 'lazy' so we need open session to read it.


In zul page there's a listbox that displays list of available resources and CmsType's name of resources (with livedata).

But when: res.getCmsType().getName() [in renderer] it throws exception
'org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed' -

So what could I do to prevent session from closing? (in this case 'org.springframework.orm.hibernate3.support.OpenSessionInViewFilter' doesn't
work)

One of solutions is to read resource again from Database in renderer but it will reduce performance.


----------------
--- list.zul ---

<?xml version="1.0" encoding="UTF-8"?>

<zk
xmlns="http://www.zkoss.org/2005/zul"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:zk="http://www.zkoss.org/2005/zk"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.zkoss.org/2005/zul
http://www.zkoss.org/2005/zul/zul.xsd">

<zscript>
<![CDATA[
import com.edirect.pl.app.cms.zk.livedata.CmsLiveData;

model = CmsLiveData.getTestListModel();
renderer = CmsLiveData.getTestListItemRenderer();
]]>
</zscript>

<listbox model="${model}" itemRenderer="${renderer}"/>

</zk>


------------------------
--- CmsLiveData.java ---

package com.edirect.pl.app.cms.zk.livedata;

class CmsLiveData
{
@SuppressWarnings( "unchecked" )
public static ListModelList getTestListModel()
{
ICMS cmsFacade = (ICMS)SpringUtil.getBean("cms");
List list =
cmsFacade.getResourceManager().getResources(CmsUtil.getWebsiteId());
return new ListModelList(list);
}


public static ListitemRenderer getTestListItemRenderer()
{
return new ListitemRenderer()
{
public void render( Listitem item, Object data ) throws Exception
{
Resource res = (Resource)data;
item.setAttribute("item",res,Component.COMPONENT_SCOPE);

Listcell cell = new Listcell(res.getName());
cell.setParent(item);

Listcell cell = new Listcell(res.getCmsType().getName());
cell.setParent(item);
}
};
}
}


---------------
--- web.xml ---

....
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilt
er</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
....

delete flag offensive retag edit

5 Replies

Sort by ยป oldest newest

answered 2007-04-04 12:24:48 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: jumperchen

Try to use Hibernate.initialize method.

You can refer to the following message.

http://www.javalobby.org/java/forums/t20533.html

Jumper

link publish delete flag offensive edit

answered 2007-04-05 06:19:10 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: grzegorzk__

Hi

I don't want to initializes all lazy associations in object (eg object may have collections with 1000000 big objects). I'm searching for something like OpenSessionInView to get lazy associations when I need them.

link publish delete flag offensive edit

answered 2007-04-06 13:07:20 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: henrichen

Did you have these lines in WEB-INF/zk.xml ?

<listener>
<description>Spring TransactionSynchronizationManager handler</description> <listener-class>
org.zkoss.zkplus.spring.SpringTransactionSynchronizationListener
</listener-class>
</listener>

/henri

link publish delete flag offensive edit

answered 2007-04-12 06:09:36 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: grzegorzk__

Hi Henri

YES, I have this lines in zk.xml

link publish delete flag offensive edit

answered 2007-04-13 04:27:19 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: henrichen

It has something to do with how you access your persistent objects.

1. load the zul page: HttpRequest, Hiberate Session1 (hb1) open, the
CmsLiveData.getTestListModel() method loads all "resources" and all resources object now associated with Hibernate Session1 (hb1). Then the hb1 is closed automatically by "OpenSessionInView" filter.

2. user drag the scrollbar. XMLHttpRequest (Ajax Request), Hibernate Session2
(hb2) open, the renderer attempt to lazy load name from the "resource" object that was loaded in another Hibernate Session1 (hb1). The Hibernate engine try to use the hb1 (since it is the Hibernate session that loaded the resource object) and complain it (hb1) is already closed.

To solve this issue, you have to "reattach" the resource object to "current"
Hibernate session (hb2) before doing the lazy load. try

session.lock(res, LockMode.NONE);

/henri




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-04-04 10:34:23 +0800

Seen: 304 times

Last updated: Apr 13 '07

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