-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Why my bean is so "lazy" ?
PostPosted: Wed Nov 16, 2005 9:31 am 
Newbie

Joined: Fri Jul 08, 2005 6:23 am
Posts: 5
Hi gurus, sorry if I ask again about lazy exceptions, but I read a lot of posts about, and still I didnt find the solution.

I have a DAO object that load an object and return it to the ActionForm.
In this action if I do: objH.getXXX() I get the lazy exception telling me that thw "owning" session is closed.

this is my DAO method:

Code:
   public PersonalAlertH getPersonalAlertsByIDAlert(Integer idAlert)
   {
      PersonalAlertH pAlert=null;
      Session hSession = factory.openSession();
      try {
         pAlert = (PersonalAlertH) hSession.load(PersonalAlertH.class,idAlert);
         
         return pAlert;
      } catch (HibernateException e) {
         e.printStackTrace();
         return null;
      }finally{
         hSession.flush();
                                                hSession.close();

      }
   }



In my action I have this:

Code:
PersonalAlertH pH = myDAO.getPersonalAlertsByIDAlert(new Integer(alertID));

int id = ph.getAlertID();  //THIS THROWS THE LAZY EXCEPTION



I mean, is it possible that I cannot return a POJO load from the db, close the hSession, and return it to the action, without getting that error ????

One workaround I found is to clone the POJO setting into the new one data from the one loaded, and return the "cloned" one to the action.
Doing that I dont get the error.

Some explanation??

Thanks a lot

Max


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 10:04 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Change your code to:

Code:
pAlert = (PersonalAlertH) hSession.get(PersonalAlertH.class,idAlert);
return pAlert;


Or change the mapping of the PersonalAlertH class to
Code:
lazy="false"
.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 10:13 am 
Newbie

Joined: Fri Jul 08, 2005 6:23 am
Posts: 5
ernst_pluess wrote:
Change your code to:

Code:
pAlert = (PersonalAlertH) hSession.get(PersonalAlertH.class,idAlert);
return pAlert;




But doing this I dont close the H8 session !!!

ernst_pluess wrote:
Or change the mapping of the PersonalAlertH class to
Code:
lazy="false"
.

HTH
Ernst


But I specified nothing into my mappin' file, so I guess it shoud be by default set to false.
You think I must specify it to lazy=false ??

Thanks

Max


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 10:57 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Do not close session in DAO, close it after transaction in Controler. Session can be a threadloacal variable (see java.lang.ThreadLocal) or old plain method parameter:

Code:
public PersonalAlertH getPersonalAlertsByIDAlert(Session hsession,Integer idAlert)
   {
      return (PersonalAlertH) hSession.get(PersonalAlertH.class,idAlert);
         
    }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 11:03 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 4:27 pm
Posts: 40
Location: canada
hibernate 3 defaults the lazy attribute to true. if you want it to be false, you have to specify it in the mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 11:56 am 
Newbie

Joined: Fri Jul 08, 2005 6:23 am
Posts: 5
baliukas wrote:
Do not close session in DAO, close it after transaction in Controler. Session can be a threadloacal variable (see java.lang.ThreadLocal) or old plain method parameter:

Code:
public PersonalAlertH getPersonalAlertsByIDAlert(Session hsession,Integer idAlert)
   {
      return (PersonalAlertH) hSession.get(PersonalAlertH.class,idAlert);
         
    }


Unfortunately in our webapp we miss DTO layer, so I directly send to the page my objectH, and of course I cannot close my H8 session in the page :) so I must close my session into the DAO method.
Anyway I solved my problem specifically setting

Code:
lazy=false


into my mappin' file.
In effect, I'm using H8 3, where jaime say that "false" is not the default.

Thanks to everyone :)

Salud

Max


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.