-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: LazyInitializationException problem
PostPosted: Thu Dec 22, 2005 2:44 am 
Newbie

Joined: Thu Dec 22, 2005 2:25 am
Posts: 6
Hi,

can i get null value instead of LazyInitializationException after closing session.

when iam using beanutils or betwixt apis iam getting LazyInitializationException.

i need to avoid that exception and should get a null value.

how can i do that...

thanks in advance...

bye...




Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0

Mapping documents:generated with middlegenide

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 5:29 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Supposing you want to avoind LazyInitializationException on
Code:
public Set getSomething(){
    return something;
}


The best way to do this, imho, is to convert your class like this:

Code:
private Set getSomethingImpl(){
    return something;
}
private void setSomethingImpl(Set something){
    this.something=something;
}
public Set getSomething(){
    try{
        return getSomethingImpl();
    } catch (LazyInitializationException e){
        return null;
    }
}
public void setSomething(Set something){
    setSomethingImpl(something);
}


Then have hibernate maps to property somethingImpl instead of something. This is how i do when i want to add logic to getters/setter but i don't want this logic to be triggered by Hibernate persitence automatisms. As the property somethingImpl as private getters/setters, beanutils shouldn't try to access them i think.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 6:20 am 
Newbie

Joined: Thu Dec 22, 2005 2:25 am
Posts: 6
Hi,

Thanks for your fine solution but with that solution i have to write two more methods for each attribute in all my bean classes.

Dont mind.

Is there any other way to avoid that exception.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 6:26 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Yes.

don't close/disconnect the Session until you have finished manipulating your datas (which seems to me quite obvious to do, don't close your session unless you are sure you won't require more datas)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 6:34 am 
Newbie

Joined: Thu Dec 22, 2005 2:25 am
Posts: 6
what im doing is getting the object loaded from DB and closing the session.

after that im passing that object to the Betwixt API to write to xml(only loaded data i wanted).but that betwixt api is parsing through the object graph.there when it just accesses the object which is not loaded there exception coming.

i wanted a way to get that object out of the context of the Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 6:55 am 
Newbie

Joined: Mon Dec 05, 2005 12:19 pm
Posts: 6
This solution ain't elegant, but you can travel your object graph and nullify uninitialized properties before passing the graph forward:

http://forum.hibernate.org/viewtopic.ph ... highlight=


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 6:56 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Then i think you'll have to handle this on betwixt part. There is most proably a way to manager exceptions in this tool.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 8:17 am 
Newbie

Joined: Thu Dec 22, 2005 2:25 am
Posts: 6
thanks garyrung....

still im not able to do it..

im not getting a way to traverse the entire object graph to nullify uninitialized variables.

see my problem..

in my sales order i have foriegnkey relation ship with customer and quotation.

in my salesorder class i have the objects of customer and quotation.

while im loading salesorder object only foriegnkeys are loading not the entire object.

i have to nullify the remaining fields in the Customer and Quoatation object.

problem is how to nullify all the fields in those objects.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 8:35 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Can't you just close/disconnect the session after your have created your xml? Hibernate is supposed to be used transparently. Am not sure inspecting your objects to know if they are loaded is a good design approach, as it mean adding lots of persistence related code in your presentation layer!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 8:43 am 
Newbie

Joined: Thu Dec 22, 2005 2:25 am
Posts: 6
if i do xml creation before closing the session that will create long chain in the xml.
i just wanted to nullify those uninitialized which are unwanted into xml file.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 9:13 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
I don't know the api you are using, can't you request this api to not go deeper than 2 levels, for example? Or to exclude some properties?

This looks quite beyond the scope of Hibernate and you are trying to use a side effect of Hibernate (fields may be uninitialized and session closed to force some Exceptions) to achieved your result. That mean if in the future the internal behaviour of Hibernate changes (imagine a future Hibernate version loading the first 3 elements of each set because someone discovers it improve performances) and you will might get a different XML :/


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 9:38 am 
Newbie

Joined: Thu Dec 22, 2005 2:25 am
Posts: 6
dear tchize,

there is no such provision to controle it traverse to particular depth.

but there is a provision to exclude some properties from accessing.

if we go thruogh this approach we need to hard code.

if that is the case i can easily nullify the properties directly because as a developer i know which is loaded and which is uninitialized.

but im searching for a way through which i can do get null values instead of getting LIE.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 10, 2006 3:25 pm 
Newbie

Joined: Tue Jan 10, 2006 3:21 pm
Posts: 3
tchize wrote:
Supposing you want to avoind LazyInitializationException on
Code:
public Set getSomething(){
    return something;
}


The best way to do this, imho, is to convert your class like this:

Code:
private Set getSomethingImpl(){
    return something;
}
private void setSomethingImpl(Set something){
    this.something=something;
}
public Set getSomething(){
    try{
        return getSomethingImpl();
    } catch (LazyInitializationException e){
        return null;
    }
}
public void setSomething(Set something){
    setSomethingImpl(something);
}


Then have hibernate maps to property somethingImpl instead of something. This is how i do when i want to add logic to getters/setter but i don't want this logic to be triggered by Hibernate persitence automatisms. As the property somethingImpl as private getters/setters, beanutils shouldn't try to access them i think.


Interesting idea, if you don't find coupling your model to Hibernate. There must be another way! :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 8:45 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
IanHlavats wrote:

Interesting idea, if you don't find coupling your model to Hibernate. There must be another way! :)


Yes, the most obvious, don't close session until you are finished using your objects.
But as the question was "can i get null value instead of LazyInitializationException after closing session." ..... :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 10:35 am 
Newbie

Joined: Tue Jan 10, 2006 3:21 pm
Posts: 3
[quote="tchize]"can i get null value instead of LazyInitializationException after closing session."[/quote]

Sure. Why not let POJOs be POJOs and set the model object property to null in a service or DAO class instead?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.