Results 1 to 6 of 6

Thread: LazyInitializationException: could not initialize proxy-the owning Session was closed

  1. #1
    Join Date
    Jan 2006
    Posts
    10

    Unhappy LazyInitializationException: could not initialize proxy-the owning Session was closed

    Code:
    org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
    org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
    org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:80)
    org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
    I have this exception when I want to retrive an object that is a foreign key in some other object.

    Object "milestone" has a property "personFk" (which is instance of Person class and is described in this way

    Code:
    /**
    	 * 
    	 * @hibernate.many-to-one column = "person_fk"
    	 * class = "business.model.Person"
    	 * not-null = "false" 
    	 */
    What can be the reason of this exception? I use hibernate 3 and MySql 5...

  2. #2
    Join Date
    Jul 2005
    Location
    Idaho
    Posts
    231

    Default

    Search the forum for OpenSessionInView (filter and interceptor - I am not sure what is the best practice) and push that through in your configuration.

    Steve

  3. #3
    Join Date
    Nov 2006
    Posts
    5

    Default

    Some posts recommend that you use get rather than load, but
    I noticed that Eclipse created a hibernate.cfg.xml and an applicationContext.xml for Spring. Both had the database objects mapped. I deleted hibernate.cfg.xml and load worked. Otherwise, I got the same error as above.

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Your point with regards to the configuration is a valid one. The issues regarding load and get though I think are to do with Hibernate proxies. If you use load your not hitting the database until something actually uses the object, get on the other hand does retrieve it. I'm sure I saw that somewhere but can't find it now.

    This is a really old thread though.

    http://www.hibernate.org/hib_docs/re...rmance-proxies
    http://forum.hibernate.org/viewtopic...81146e8e6c1b58

  5. #5

    Default

    Hi,

    I've just had a similar problem and can confirm that hibernateTemplate.get(....) should be used instead of load(..) when you don't need to load it lazily.

    With load you need to re-attach the owning session somehow, which is only possible by locking it to the saved object. You don't really want to do that if your using the OpenSessionInViewFilter / Interceptor for your views as you will end up with a session remaining locked open.

    Hope thats some help to anyone reading this thread.

    John.

  6. #6
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by karldmoore View Post
    The issues regarding load and get though I think are to do with Hibernate proxies. If you use load your not hitting the database until something actually uses the object, get on the other hand does retrieve it.
    I can confirm it, load() just creates a proxy and does not hit the database while get() does. I had that issue with my HibernateEntityPropertyEditor where I wanted to use load() but needed to switch to get() because of LazyInitializationException during binding (what makes sense).

    Joerg
    This post can contain insufficient information.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •