-->
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: Remote lazy initialization in a client-server app
PostPosted: Mon Dec 08, 2003 10:59 am 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
Hi all,

I have tried to figure out from the other postings what is the proper way to use lazy init in a client server architecture. I want to have a (possible) thin client who gets DTOs from a server via J2EE session beans.
I would prefer that only the server be aware of the persistence(Hibernate).

Now, if I want to use lazy in the client, say for object A that has lazy collection B, how can I retrieve B from the server?

Will A.getB() trigger an action to retrieve the collection from the server?

Any help is much appreciated.

TIA,

--steve p.


Top
 Profile  
 
 Post subject: Re: Remote lazy initialization in a client-server app
PostPosted: Mon Dec 08, 2003 11:12 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
dia100 wrote:
Will A.getB() trigger an action to retrieve the collection from the server?

You'll get a LazyInitializationException, since your session is disconnected.
You need to give your cleint the whole graph or manage the lazy init exceptions.
What Hibernate do is reattach a graph previously given to a client layer

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Remote lazy initialization in a client-server app
PostPosted: Mon Dec 08, 2003 11:23 am 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
epbernard wrote:
dia100 wrote:
Will A.getB() trigger an action to retrieve the collection from the server?

You'll get a LazyInitializationException, since your session is disconnected.
You need to give your cleint the whole graph or manage the lazy init exceptions.
What Hibernate do is reattach a graph previously given to a client layer


Thx for the quick answer. Still I am unclear:

Is it possible to open a session via JNDI in the client, try to do A.getB()
and then close the session from the client ? Will it get the collection from the server?

thx,

--steve p.


Top
 Profile  
 
 Post subject: Re: Remote lazy initialization in a client-server app
PostPosted: Mon Dec 08, 2003 12:00 pm 
Newbie

Joined: Tue Nov 25, 2003 5:05 pm
Posts: 18
dia100 wrote:
Thx for the quick answer. Still I am unclear:

Is it possible to open a session via JNDI in the client, try to do A.getB()
and then close the session from the client ? Will it get the collection from the server?


AFAIK this is not possible with Hibernate. The functionality you're looking for is mostly OODBMS stuff. OODBMS were designed to handle navigational access in a optimized way.

ciao

Sven


Top
 Profile  
 
 Post subject: Re: Remote lazy initialization in a client-server app
PostPosted: Mon Dec 08, 2003 12:07 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
dia100 wrote:
Is it possible to open a session via JNDI in the client, try to do A.getB()
and then close the session from the client ? Will it get the collection from the server?


Don't know, I read somewhere that hibernate session (or session factory) were limited to local purpose only. May someone confirm ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Remote lazy initialization in a client-server app
PostPosted: Mon Dec 08, 2003 12:07 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
Sven! wrote:
dia100 wrote:
Thx for the quick answer. Still I am unclear:

Is it possible to open a session via JNDI in the client, try to do A.getB()
and then close the session from the client ? Will it get the collection from the server?


AFAIK this is not possible with Hibernate. The functionality you're looking for is mostly OODBMS stuff. OODBMS were designed to handle navigational access in a optimized way.

ciao

Sven


Thx for the answer!

To conclude:
What would be the proper way to get lazy collections from a J2EE server backed by Hibernate in a client?

b.t.w:
How does CMP cope with this (getting lazy collections from the server) ?

TIA,

--steve p.


Top
 Profile  
 
 Post subject: Followup
PostPosted: Wed Jul 14, 2004 4:58 pm 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
This post is similar to what I am trying to do.

In the case where you can't handle the LazyInitializationException in the code.. it would be nice to be able to apply a ExceptionInterceptor to a Persistent class or something such that Hibernate (or custom intercepter) will seemlessly handle the exception catching and rebind the object to a new or current session. I feel there would be a lot of applications for such a design.

For instance in my post below, I mention the use of the DisplayTag taglib. Unless you get in and modify the code to recognize hibernate exceptions and do the rebinding... there is no easy way to intercept the exception at the JSP level. Currently the only way around this is to preload everything, but say for the sake of a simple example you have the following scenario:


A list of LOAN(s) is returned to the jsp and the DisplayTag iterates a users loans and displays basic details such as: id, date, amount. This is all good, and no issues yet. But if you decide to allow the user to do an csv export using the DisplayTag feature. The export includes all attributes of LOAN; those display on the table (preloaded) plus a LOAN_ACCOUNT_DETAIL reference which was not preloaded.

If this export feature was to be used by all users then it would make sense to preload the LOAN_ACCOUNT_DETAIL(s) but if it is a rarity that it is called but you still need the feature, it would be better to have it loaded ON DEMAND such is the point lazy instantiation.

So having a facility to make this happen (invisibly via way of configuration or a custom Interceptor) would be a great idea I would think. Having to modify the DisplayTag (for instance) tag to handle the Hibernate Exception would not be optimal.

Since the DisplayTag code is on the Server we should be able to somehow bind the object to a new session.

Is there already a way to do this.. similar to the Callback methods (onSave, onDelete, etc). I am really not sure how without reworking Hibernate, and whatever method throws the LazyInitializationException, this could be done.


Troy

---------- reposted -----------------

I have been reading various posts on this topic. The most significant being: http://www.hibernate.org/124.html

I was wondering is there some pattern or design to allow lazy instantiation failover? Such as from the UI.

The very simplest would be have a configuration such that you could catch LazyInstantiationException and then take the suspect object and lock it to a newly created session.

One example I have of this is using the DisplayTag TagLib. If I don't preload the entire bean list that I pass to the UI it will throw an exception if I try to do an XML export etc. To work around this I have only found 2 solutions. The first is to preload every attribute of each bean at the DAO level when I retrieve them. This is obvously not a great solution. The second is to call a specific method in my DAO to preload specific attributes of the beans. This also is not the best since I have to anticipate what attributes will be accessed.

Another solution that I am considering is to somehow intercept the 'LazyInitializationException' and have the framework auto associate the object with a new or current session. I am not sure how to approach this exactly and would welcome comments.

Thanks

Troy McKinnon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 14, 2004 5:00 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Guys, the idea sounds good, but it doesn't work. What will Hibernate do if it magically reattaches the instance to load a lazy association and finds that the original object is no longer in the database? This "I don't care about database transactions" approach is not going to work.

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


Top
 Profile  
 
 Post subject: StaleDataException
PostPosted: Wed Jul 14, 2004 6:23 pm 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
I would suggest a StaleDataException, but I guess this is just digging us deeper.

I think the concept of an intercepter would still work however.
You could associate a interceptor to a session or set of objects.

If you had a number of fallback strategies you could design the interceptor as such.

i.e. LazyInitializationException --> attempt to reload throw StaleDataException if data changed.


But I digress, I guess I am just digging the hole deeper...

Guess there isn't really a solution to this other then case by case handling via catching of the Lazy Exception, or force loading.


Troy


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 15, 2004 2:10 am 
Newbie

Joined: Thu Jul 15, 2004 2:02 am
Posts: 2
Hi,
I am new to this just forum.
I am having a problem with lazy initialization.
There are a few objects (Example objA) used by many other objects and objA has some child classes which are loaded with objA. This objA is used in atleast 10 other classes and probably 2 or 3 of them use this child class data. I tried uisng this lazy initialization by giving lazy = "true" but i get the same error, no session.
If we look in the net, they say that there are 3 options for this :
* Either keep the session open until all the lazy objects are loaded
* Preload all of them
* Reattach by calling lock() or update() a Detached Object
* Initialize the lazy collections manually with initialize() before closing the session.

Right now we are using the 2nd option. The 3rd option seems okay, but how and when do i have to call the method lock() or update().

Can anyone help me in this?

JK


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.