-->
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: LazyInitializationException on a property
PostPosted: Mon Sep 24, 2007 11:23 am 
Newbie

Joined: Thu Jul 19, 2007 3:53 pm
Posts: 7
I'm having issues with getting a property of an object throwing a LIE.
pojo:
Code:
public class EventTypeVO {
    private String description;
    public String getDescription()...
    public void setDescription()..
}

I first call a:
Code:
session.createQuery("from EventTypeVO").list();

which returns a list. I'm able to iterate through this list on my jsp and call:
Code:
eventTypeVO.getDescription();

without any problems. The descriptions display fine. When an eventType is selected, the call goes to a load with the eventTypeId..
Code:
session.load(EventTypeVO.class, eventTypeId);

When I try and access the description (either in the biz or jsp) I get this error:
Code:
LazyInitializationException: could not initialize proxy - the owning Session was closed
EventTypeVO$$EnhancerByCGLIB$$a50e6959.getDescription(<generated>)

I'm lost as to why accessing a property would throw this.
Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 24, 2007 11:35 am 
Beginner
Beginner

Joined: Fri May 18, 2007 10:28 am
Posts: 48
Location: Madison, WI
You are probably closing the session. Can you verify is session is open when u loop through the list.

_________________
Please rate if it helped


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 24, 2007 12:02 pm 
Newbie

Joined: Thu Jul 19, 2007 3:53 pm
Posts: 7
The list works fine. Its when a specific one is loaded that the error happens.
Code:
session = HibernateUtil.currentSession();
EventTypeVO eventTypeVO = session.load(EventTypeVO.class, eventTypeID);
session.closeSession();
return eventTypeVO;

This should load all of the properties (columns) from the table. My question is, why isn't it?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 25, 2007 2:34 am 
Newbie

Joined: Tue Sep 25, 2007 2:15 am
Posts: 2
Location: india
according to me,

when you call ur load function

session = HibernateUtil.currentSession();
[b]EventTypeVO eventTypeVO = session.load(EventTypeVO.class, eventTypeID); [/b]
session.closeSession();
[b]return eventTypeVO;[/b]

the return gives you a proxy of EventTypeVO and not the actual object.This is probably the reason why you are getting the lazy exception.After you call the load function,before returning the VO,fire a getter for any property like

eventTypeVO.getDescription();

This will load the actual values from the table.

you are probably using hibernate version 3.....lazy is false by default in version 3.You will not get this problem in version 2 where lazy is true by default.

_________________
Thanks,
Vikram


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 25, 2007 8:30 am 
Newbie

Joined: Thu Jul 19, 2007 3:53 pm
Posts: 7
Thanks Vikram for the reply.
So in version 3 lazy applies not only to relationships but properties as well? Is there an attribute I could set in the hbm file to eagerly load the properties, similar to relationships?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 25, 2007 8:44 am 
Newbie

Joined: Tue Sep 25, 2007 2:15 am
Posts: 2
Location: india
Yes...u probably can set the lazy property in the hbm file.

for instance,

<property name="othercaseDesc" type="string" [b]lazy="false">[/b]
<column name="OTHERCASE_DESC">
<comment></comment>
</column>
</property>

u can set this parameter false for all the properties and even relationships which u want to access eagerly.

I have not tried this myself but i think this might help you out.....

_________________
Thanks,
Vikram


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.