0

Hibernate lazy problem

asked 2008-03-26 11:41:27 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: zemo3320

Hi to all,

I have a problem regarding ZK and Hibernate, tried with two different DBMSs, MySQL and Postgre.

The problem lies in lazy initialization in some cases, most things work just fine. Theese two lines of code are together:

ProviderOwner owner = provider.getProviderOwner(); // this line is ok, provider is the method parameter OwnerGroup ownerGroup = owner.getOwnerGroup(); // this line throws the exception

The exception is:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

I have followed this article configuring Hibernate:
http://www.zkoss.org/smalltalks/hibernatezk/hibernatezk.dsp. Hibernate.cfg.xml and zk.xml seem to follow the article. All the mapping and DAO files are similar to theese below. The Java class for hibernate is also similar to those in the examples. By turning lazy off the application works (no exceptions), but is unacceptably slow.

What am I missing here? Any ideas? I've searched some of the forums but nothing seemed to help me. I can provide more code or config files if needed. Thanks in advance!


Hibernate mapping file:

_

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 12.2.2008 9:24:21 by Hibernate Tools 3.2.0.CR1 --> <hibernate-mapping>
<class name="net.example.db.Regions" table="regions" catalog="example">
<id name="idRegion" type="java.lang.Integer">
<column name="id_region" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="name" length="70" not-null="true" />
</property>
<property name="description" type="string">
<column name="description" length="65535" />
</property>
<property name="shortName" type="string">
<column name="short" length="10" not-null="true" />
</property>
<set name="citieses" inverse="true">
<key>
<column name="id_region" />
</key>
<one-to-many class="net.example.db.Cities" />
</set>
<set name="searchFilters" inverse="true">
<key>
<column name="id_region" />
</key>
<one-to-many class="net.example.db.SearchFilter" />
</set>
</class>
</hibernate-mapping>

The DAO class:


package net.example.db;

import java.util.List;
import org.apache.log4j.Logger;
import org.zkoss.zkplus.hibernate.HibernateUtil;

public class RegionsDAO
{
private static Logger log = Logger.getLogger(RegionsDAO.class);
private org.hibernate.Session session;

public RegionsDAO()
{
session = HibernateUtil.getSessionFactory().getCurrentSession();
}

public void saveOrUpdate(Regions regions)
{
session.saveOrUpdate(regions);
}

public void delete(Regions regions)
{
session.delete(regions);
}

public Regions findById(Long id)
{
return (Regions) session.load(Regions.class, id);
}

public List findAll()
{
return session.createQuery("from Regions").list();
}
}


delete flag offensive retag edit

1 Reply

Sort by ยป oldest newest

answered 2008-03-26 13:25:26 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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

By: migu82

Hey, this problem is does not concern ZK, it is a Hibernate specific problem.
You are trying to access a property of an class outside of the transaction.
Meaning, Hibernate loads only one object if the lazy loading is set to true, and all associations are loaded only when you access them, but this can be only done inside the transaction scope. You have to call getProviderOwner() while in transaction (meaning in DAO or Service if service is bound to transactions).
This is the most common misuse of Hibernate.

Still, I recommend to post these questions in:
http://forum.hibernate.org/

Miku


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: 2008-03-26 11:41:27 +0800

Seen: 312 times

Last updated: Mar 26 '08

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