-->
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.  [ 4 posts ] 
Author Message
 Post subject: does J2EE application go through twice?
PostPosted: Sat Jun 03, 2006 6:05 pm 
Newbie

Joined: Sun May 14, 2006 5:09 pm
Posts: 11
in a web based J2EE application,I try to get a list trough:JSP->EJB->DAO. in the DAO, I use Hibernate to load a list of object,then in the JSP,I get and display each element .


I'm confused with Hibernate lazy fetch
according to lazy fetch, the first query only load non-associated non lazy objects. lazy objects are only loaded when it is first used.

before display each element, I get the list,Hibernate doesn't load associated entities and collections in the list. when I use get method to display the element wich is not loaded in the list,does application go through JSP->EJB->DAO again to load those associate elements?


if so, I will be more confused. the lazy fetcing is Hibernate feature,it should not change the J2EE(JSP,EJB) working flow.

Thank you for clearify.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 03, 2006 7:49 pm 
Beginner
Beginner

Joined: Sat Jun 03, 2006 6:23 pm
Posts: 28
Under some circumstances you will get Proxy objects instead of your normal "DAO" objects (loading via primary key, lazy associations). If you don't initialize them (e.g. with Hibernate.initialize) before the session closes, they will be disconnected.

If you try to access any unloaded fields of a disconnected Proxy, it will try to initialize a real object (proxies tipically can return only their primary key without such initialization occuring), and of course fail.

So if you have your jsp running in the same container as your ejb-s, and your session is closed only after the jsp is finished, the jsp will be able to initialize the proxy, because the session is still there.

On the other hand, and this is generally the case, if your ejb-s commit the transaction and close the session before returning data (possibly to a remote machine), your session will not be there, and initialization of the proxy will fail with an exception. You will need to initialize all proxies that you intend to use before returning from the ejb call.
Quote:
it should not change the J2EE(JSP,EJB) working flow

Hibernate does not change anything. You simply need to load data you want to use while your connection is still open, just like with any data access mechanism. After the connection (your Session in this case) is closed, lazy objects will be uninitialized, thus mostly unusable (other than their primary key). Hibernate does no extra "magic" for you, like calling an ejb for more data.

Roland


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 03, 2006 10:29 pm 
Newbie

Joined: Sun May 14, 2006 5:09 pm
Posts: 11
Thank you for reply
let's say everything I'm doing is before session closed, what's will be happend is :

1.
JSP to load list -> EJB -> DAO(load list)
JSP to display element
JSP to display lzay entities -> EJB -> DAO (load lazy entity)

or

2.
JSP to load list -> EJB -> DAO (load list ,load lazy entities)

if 1 is true, there will be 2 round trip from JSP to EJB to DAO
if 2 is true , how does hibernate(in DAO) knows JSP WILL use those lazy entities.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 04, 2006 5:17 am 
Beginner
Beginner

Joined: Sat Jun 03, 2006 6:23 pm
Posts: 28
Quote:
1.
JSP to load list -> EJB -> DAO(load list)
JSP to display element
JSP to display lzay entities -> EJB -> DAO (load lazy entity)


This is definitely not the case. The proxy object has a reference to a Session (~Database connection). If it needs to be initialized, it uses this Session to do that, it never accesses EJB-s automatically (how could it?). If the Session is disconnected, initialization will simply fail with an exception, it will not call EJBS-s, it won't attempt to connect to the database. Sessions are only opened when you say so specifically. (Well, except in managed environments, but still, there you specify this declaratively)
Quote:
2.
JSP to load list -> EJB -> DAO (load list ,load lazy entities)

if 1 is true, there will be 2 round trip from JSP to EJB to DAO
if 2 is true , how does hibernate(in DAO) knows JSP WILL use those lazy entities.


Hibernate does not know, and I think it does not care. If you try using uninitialized proxies, you will end up with an exception.
You will need to specifically initialize proxies at a point in your code where you still have an open session. If that place is the EJB, thats where it needs to be done. If your session is still open in your JSP, you can let lazy initialization happen there (don't distribute your application on different containers in that latter case, though).

I have the impression that you think lazy objects can do all kinds of magic for you. Well, that is not the case. Lazy objects have many advantages, but automagically fetchin data without database connection is not one of them. In fact, that would be totally confusing if they just tried to open database connections as they see fit.


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