Announcement Announcement Module
Collapse
No announcement yet.
LazyInitializationException: could not initialize proxy-the owning Session was closed Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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.

      Comment


      • #4
        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

        Comment


        • #5
          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.

          Comment


          • #6
            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

            Comment

            Working...
            X