You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Juan Esteban Maya <jm...@gmail.com> on 2005/05/17 13:00:48 UTC

Hibernate + Spring: LazyInitializationException

Hi..
I have been trying to use Tapestry with Spring + Hibernate 3.
I have follow the wiki help about this issue, but i keep getting a
LazyInitializationException everytime i load a lazy relation. Is there
anything more needed to get hibernate lazy loading working?

Thanks in advance

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Hibernate + Spring: LazyInitializationException

Posted by Didier LAFFORGUE <di...@capgemini.com>.
Completely, I agree with you. Moreover, in my example, when you create a new session, you have to reattach the main object (employee) and its dependencies (company). So, if you want to update the "employee", you should do:

---------------------------------------------------
Session session = HibernateFactory.getCurrentSession();

Employee employee = session.load(getEmployeeId());
Company company = session.load(getCompanyId());
employee.setCompany(company);
session.save(employee); // I'm not sure of the syntax.
---------------------------------------------------

What is your opinion about it Markus ?


    Did

----- Original Message -----
From: "Markus Eberle" <Ma...@tngtech.com>
To: "Tapestry users" <ta...@jakarta.apache.org>; "Didier LAFFORGUE" <dl...@capgemini.fr>
Cc: "Tapestry users" <ta...@jakarta.apache.org>; "Juan Esteban Maya" <jm...@gmail.com>
Sent: Wednesday, May 18, 2005 2:05 PM
Subject: Re: Hibernate + Spring: LazyInitializationException


> Zitat von Didier LAFFORGUE <di...@capgemini.com>:
>
> >
> > I think that the LazyInitializationException occurs when you try to access a
> > property of your object outside the hibernate session.
> > In fact, imagine you have 2 mapped objects: employee and company => in this
> > example, an employee has only one company.
> > First of all, you load the employee with the id 1. In the lazy mode, the
> > associated company is not loaded.
> > If you do not close the current hibernate session, if you do a
> > "employee.getCompany()", hibernate will use the session to load the company
> > object.
> > BUT if you close the session and after if you do the "employee.getCompany()"
> > statement, you'll get the LazyInitializationException.
> > The solution would be to keep the hibernate session in the session scope but
> > I find this solution very dirty.
>
> This _is_ very dirty.
>
> > Another solution is to create the session
> > when the page begins to render and to close it when the rendering is finished
> > (not tested so....).
>
> In most cases you can use the OpenSessionInViewFilter (i think this is the name)
> from spring.
> The problem is, that you get a new Hibernate-Session, so you have to re-attach
> the objects if you re-use them.
>
> Cheers,
>     Markus
>
>
> > ----- Original Message -----
> > From: "Juan Esteban Maya" <jm...@gmail.com>
> > To: "Tapestry users" <ta...@jakarta.apache.org>; "pepone pepone"
> > <pe...@gmail.com>
> > Sent: Tuesday, May 17, 2005 1:00 PM
> > Subject: Hibernate + Spring: LazyInitializationException
> >
> >
> > > Hi..
> > > I have been trying to use Tapestry with Spring + Hibernate 3.
> > > I have follow the wiki help about this issue, but i keep getting a
> > > LazyInitializationException everytime i load a lazy relation. Is there
> > > anything more needed to get hibernate lazy loading working?
> > >
> > > Thanks in advance
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> > >
> >
> > This message contains information that may be privileged or confidential and
> > is the property of the Capgemini Group. It is intended only for the person to
> > whom it is addressed. If you are not the intended recipient,  you are not
> > authorized to read, print, retain, copy, disseminate,  distribute, or use
> > this message or any part thereof. If you receive this  message in error,
> > please notify the sender immediately and delete all  copies of this message.

This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient,  you are not authorized to read, print, retain, copy, disseminate,  distribute, or use this message or any part thereof. If you receive this  message in error, please notify the sender immediately and delete all  copies of this message.


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Hibernate + Spring: LazyInitializationException

Posted by Markus Eberle <Ma...@tngtech.com>.
Zitat von Didier LAFFORGUE <di...@capgemini.com>:

>
> I think that the LazyInitializationException occurs when you try to access a
> property of your object outside the hibernate session.
> In fact, imagine you have 2 mapped objects: employee and company => in this
> example, an employee has only one company.
> First of all, you load the employee with the id 1. In the lazy mode, the
> associated company is not loaded.
> If you do not close the current hibernate session, if you do a
> "employee.getCompany()", hibernate will use the session to load the company
> object.
> BUT if you close the session and after if you do the "employee.getCompany()"
> statement, you'll get the LazyInitializationException.
> The solution would be to keep the hibernate session in the session scope but
> I find this solution very dirty.

This _is_ very dirty.

> Another solution is to create the session
> when the page begins to render and to close it when the rendering is finished
> (not tested so....).

In most cases you can use the OpenSessionInViewFilter (i think this is the name)
from spring.
The problem is, that you get a new Hibernate-Session, so you have to re-attach
the objects if you re-use them.

Cheers,
    Markus


> ----- Original Message -----
> From: "Juan Esteban Maya" <jm...@gmail.com>
> To: "Tapestry users" <ta...@jakarta.apache.org>; "pepone pepone"
> <pe...@gmail.com>
> Sent: Tuesday, May 17, 2005 1:00 PM
> Subject: Hibernate + Spring: LazyInitializationException
>
>
> > Hi..
> > I have been trying to use Tapestry with Spring + Hibernate 3.
> > I have follow the wiki help about this issue, but i keep getting a
> > LazyInitializationException everytime i load a lazy relation. Is there
> > anything more needed to get hibernate lazy loading working?
> >
> > Thanks in advance
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >
>
> This message contains information that may be privileged or confidential and
> is the property of the Capgemini Group. It is intended only for the person to
> whom it is addressed. If you are not the intended recipient,  you are not
> authorized to read, print, retain, copy, disseminate,  distribute, or use
> this message or any part thereof. If you receive this  message in error,
> please notify the sender immediately and delete all  copies of this message.


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Hibernate + Spring: LazyInitializationException

Posted by Didier LAFFORGUE <di...@capgemini.com>.
I think that the LazyInitializationException occurs when you try to access a property of your object outside the hibernate session.
In fact, imagine you have 2 mapped objects: employee and company => in this example, an employee has only one company.
First of all, you load the employee with the id 1. In the lazy mode, the associated company is not loaded.
If you do not close the current hibernate session, if you do a "employee.getCompany()", hibernate will use the session to load the company object.
BUT if you close the session and after if you do the "employee.getCompany()" statement, you'll get the LazyInitializationException.
The solution would be to keep the hibernate session in the session scope but I find this solution very dirty. Another solution is to create the session when the page begins to render and to close it when the rendering is finished (not tested so....).
I hope that my explanation was rather clear ;-)))

  Did


----- Original Message -----
From: "Juan Esteban Maya" <jm...@gmail.com>
To: "Tapestry users" <ta...@jakarta.apache.org>; "pepone pepone" <pe...@gmail.com>
Sent: Tuesday, May 17, 2005 1:00 PM
Subject: Hibernate + Spring: LazyInitializationException


> Hi..
> I have been trying to use Tapestry with Spring + Hibernate 3.
> I have follow the wiki help about this issue, but i keep getting a
> LazyInitializationException everytime i load a lazy relation. Is there
> anything more needed to get hibernate lazy loading working?
>
> Thanks in advance
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>

This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient,  you are not authorized to read, print, retain, copy, disseminate,  distribute, or use this message or any part thereof. If you receive this  message in error, please notify the sender immediately and delete all  copies of this message.


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Hibernate + Spring: LazyInitializationException

Posted by Pablo Ruggia <pr...@gmail.com>.
I've not found any reasonable, easy and transparent solution to this.
So I turn off lazy initializations and try to map as less collections as I can.

But if you want to investigate, here is a link to a solution using
Tapestry + Spring + Hibernate  (I really don't like it):

http://wiki.apache.org/jakarta-tapestry/FrequentlyAskedQuestions/SpringHibernate

 

On 5/17/05, Juan Esteban Maya <jm...@gmail.com> wrote:
> Hi..
> I have been trying to use Tapestry with Spring + Hibernate 3.
> I have follow the wiki help about this issue, but i keep getting a
> LazyInitializationException everytime i load a lazy relation. Is there
> anything more needed to get hibernate lazy loading working?
> 
> Thanks in advance
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Hibernate + Spring: LazyInitializationException

Posted by Nathan Kopp <nk...@mailshell.com>.
This is a tough one.  IMHO, it is a weakness of Hibernate.

Anyway, "fixed" the problem by modifying Hibernate.  (Yes, I have customized
and misused both Tapestry and Hibernate to serve my own purposes... ah, the
beauty of open source!)  I made modifications to
AbstractPersistentCollection and AbstractLazyInitializer so that they would
open a new session (then close it later) instead of throwing an exception.
Right now my custom versions contain some application-specific code to
access my SessionFactory (which is stored in a singleton object), since I
haven't yet come up with a cleaner solution.

-Nathan


----- Original Message ----- 
From: "Juan Esteban Maya" <jm...@gmail.com>
To: "Tapestry users" <ta...@jakarta.apache.org>; "pepone pepone"
<pe...@gmail.com>
Sent: Tuesday, May 17, 2005 7:00 AM
Subject: Hibernate + Spring: LazyInitializationException


Hi..
I have been trying to use Tapestry with Spring + Hibernate 3.
I have follow the wiki help about this issue, but i keep getting a
LazyInitializationException everytime i load a lazy relation. Is there
anything more needed to get hibernate lazy loading working?

Thanks in advance


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org