-->
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.  [ 10 posts ] 
Author Message
 Post subject: lazy loading exceptions in proxies
PostPosted: Sat Nov 22, 2003 12:31 pm 
Regular
Regular

Joined: Thu Aug 28, 2003 10:54 am
Posts: 67
Currently I am loading proxies which i initialize with the properties I need in order to display in my presentation layer. Depending on the view , different properties need to be initialized. I have a converter class that converts the proxy into a useful view bean. This is where the problem is as the proxy is outside the session now.

Since the converter doesn't know what view is being constructed it needs to check if a particular fields in the proxy has been initialized or not. This requires it uses Hibernate.initialize or catched Lazy initialization exceptions both of which make my view layer depend on hibernate. Something I am not happy about. What would be ideal is for the proxy to give null values for anything that has not been initialized after the session is closed.

Alternatively, I can write a converter for each type of view but this is a lot of work with a lot of duplication.How can i use proxies and remove the dependance on hibernate from my view?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 1:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
let the session be open while the view is being rendered.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 1:36 pm 
Regular
Regular

Joined: Thu Aug 28, 2003 10:54 am
Posts: 67
No can do. The data access is done by a DAO layer. I don't know how to tell it to keep a sesion open until a view is displayed. I am also not fond of having so much reliance on hibernate outside the data access layer. Currently, I am considering having converters in the DAO layer that convert proxies into equivalent objects but with nulls for non-initialized properties.

I suppose the arguement of lazy initialization exceptions vs nulls has been done to death.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 2:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Yes - it gets' discussed often ;)

One way people handles this is to put the current session in a threadLocal and then have a service or command like pattern that allows you to explicit embed the stuff that needs access to the session.

But what is wrong about throwing an exception if the data is not valid ?
Returning null is wrong - since null is an allowed value!

If the view needs to access the data, why don't you fetch it in your DAO ?
What would your view do to distinguish between "not there" data and a real null value ?

That is why the exception is thrown.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 2:57 pm 
Regular
Regular

Joined: Thu Aug 28, 2003 10:54 am
Posts: 67
The data the view needs is fetched in the dao. its just that there are several views and each needs a different slice of the data. Nulls aren't a problem as the each view won't try to display fields its not interested in.

Our problem is that before the object gets to the view we create another one based on it that makes it easier and faster for the view to display its results ( by converting data to strings and doing formating etc...) The class that does the conversion is very general, that is, it doesn't know or care what view the data is going to. All it knows is how to convert each business object into an equivalent object that all views can display ( though they will generally only display a part of it). Now for us, testing nulls and is easier than using Hibenate.initialize on each field and also removes hibernate specific stuff from our presentation layer.

I will play a bit with threadlocal. I am still unclear as to how to tell the dao that the view has been rendered and that it can close the session. We are also considering having the dao create a new object based on the proxy that returns a special NULL object for any fields that don't have values set and use null for fields that have not been initialized.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 3:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
"Nulls aren't a problem as the each view won't try to display fields its not interested in." ...then the thrown exception ain't a problem (as you point out ya'self ;)

"The class that does the conversion is very general, that is, it doesn't know or care what view the data is going to. All it knows is how to convert each business object into an equivalent object that all views can display"...
this sounds as something that could be put in behind the DAO "session wall"

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 3:33 pm 
Regular
Regular

Joined: Thu Aug 28, 2003 10:54 am
Posts: 67
yes you're right, the view would not trigger an exception. however we do not pass the object directly to the view but to a converter/formatter object. That is the one that triggers the exception. We could have a different converter for each view but they would all be so similar that its just not worth it.

As for moving it behind the dao. We don't want the specifics of how we are formatting data for presentation to be in our data access layer. I think we will create wrappers around the proxies and pass those to the formatters.

But this brings up a possible enhancement to hibernate. How about specifying in the config file whether to return a special user-defined type if a field has not been initialized. If no setting then hibernate works the same as now , with the setting, hibernate proxy returns user-defined object. won't work for primitive types but at least its something. I am happy to add this request to JIRA if gavin thinks its practical.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 3:34 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Sounds useful, +1.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 3:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Gavin needs to recuperate from the Rugby World Cup Final - so add it to JIRA and see what happens ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 3:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
And it can work for primitives also - just works as a special value...


...but besides that - putting in such "special types/values" might mess up reattachment of the objectgraph - probably very messy.

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.