-->
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.  [ 3 posts ] 
Author Message
 Post subject: BASIC: Lazy Loading Question
PostPosted: Wed Jan 12, 2005 1:33 pm 
Newbie

Joined: Mon Jan 10, 2005 2:33 pm
Posts: 4
Hi! I hope this question deserve your attention and answer.

I got the following secuence:

- Open my session
- Get a list of objects using lazy loading
- Close the Session

The user pick one of those objects.
I try to load the pending objects (those ones related to this by many-to-one mapping) using the follwing code:

Code:
public void resolveLazyLoading(Object o){
   
   Ccliente c=(Ccliente)o;
   
   Session s = null;
   try {
      s = getSession();
      s.lock(c,LockMode.NONE);
      
      Integer cd=(c.getIddireccionbodega()!=null)?c.getIddireccionbodega().getIddireccion():null;
      cd=(c.getIddireccionfiscal()!=null)?c.getIddireccionfiscal().getIddireccion():null;
      c.getIddireccionoficina().getIddireccion();
      c.getIdempleado().getIdempleado();
      c.getIdsucursal().getIdsucursal();
      
   } catch (HibernateException e){
      e.printStackTrace();
   }finally {
      try {
         closeSession();
      } catch (HibernateException e1) {}
   }
}


Then When I try to use the object's properties, say c.getIddireccionoficina().getIddireccion();, I get the following error:

Code:
11:03:51,046 ERROR LazyInitializer:63 - Exception initializing proxy
net.sf.hibernate.HibernateException: Could not initialize proxy - the owning Session was closed
   at net.sf.hibernate.proxy.LazyInitializer.initialize(LazyInitializer.java:47)
   at net.sf.hibernate.proxy.LazyInitializer.initializeWrapExceptions(LazyInitializer.java:60)
   at net.sf.hibernate.proxy.LazyInitializer.getImplementation(LazyInitializer.java:164)
   at net.sf.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:108)
   at net.sf.hibernate.proxy.HibernateProxy$$EnhancerByCGLIB$$61ab85cc.toString(<generated>)
   at com.ia.ventas.ui.UIPedidoDetalle.muestraCliente(UIPedidoDetalle.java:1043)
   at com.ia.ventas.ui.UIPedidoDetalle.inicializaVentana(UIPedidoDetalle.java:875)
   at com.ia.ventas.ui.UIPedidoDetalle.createDialogArea(UIPedidoDetalle.java:803)
   at org.eclipse.jface.dialogs.Dialog.createContents(Dialog.java:632)
   at org.eclipse.jface.window.Window.create(Window.java:348)
   at org.eclipse.jface.dialogs.Dialog.create(Dialog.java:925)
   at org.eclipse.jface.window.Window.open(Window.java:637)
   at com.ia.ventas.ui.UIPedido.agregaPedido(UIPedido.java:549)
   at com.ia.ventas.ui.UIPedido.access$2(UIPedido.java:536)
   at com.ia.ventas.ui.UIPedido$6.widgetSelected(UIPedido.java:224)
   at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:89)
   at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:82)
   at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:796)
   at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:2772)
   at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2431)
   at com.ia.Menu.main(Menu.java:112)



My question is I have to keep the session open all the time even when I already use those object's properties and supposedly they already loaded their state from database?

Thanks in advance!!
Rodolfo


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 5:57 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
If you are using Lazilly loaded collections or your object has a relationship to a proxied Hibernate object, your session must be open when you access the lazy property. Here's why:

For lazy collections Hibernate populates the collection with a set of "proxy" objects that only contain the ID and Version of the object. When you access the proxied object's non ID/Version properties the first time the data for the object is loaded from the DB and the proxy gets "filled in" with the real Hibernate object. This being the case, if your Hibernate session is closed, there's no way that the lazy data can be read.

This problem also happens with object relationships where the other side of the relationship is a Proxied object. In this case the related object is only loaded when a property of the object other than the ID or Version is accessed for the first time.

The solution is to keep your Hibernate Session open. If you're writing a GUI then the ThreadLocal pattern can work well. If you're writing a Web application then the "Open Session In View" pattern can work well also. I've used both of these with great success. The idea is that a Hibernate Session is associated with the currently running thread. When the Thread goes out of a certain scope, the session is closed automatically. In the Open Session In View case, the Session is managed by a Servlet Filter that is responsible for opening a session at the beginning of the request and closing it at the end.

There are MANY posts and resources available on this topic. Search the Hibernate site and you'll find them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 5:51 pm 
Newbie

Joined: Mon Jan 10, 2005 2:33 pm
Posts: 4
Thanks for your answer!!

I going to implement the patterns.


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