Announcement Announcement Module
Collapse
No announcement yet.
What is that I am missing? (LazyInitializationException) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is that I am missing? (LazyInitializationException)

    Hi,

    I know this is to do with the Hibernate Session. But I am not sure how to solve this.
    Code:
    class Person {
    	String firstName;
    	String lastName;
    	Set addresses;
    }
    class Address {
    	String addressLine1;
    	String city;
    }
    This is basically a one-to-many relationship, where the address is marked
    as lazy=”true” in the hbm.xml file.

    In my DAO, I have a method to find a Person given the primary key.

    Code:
    public class PersonDAOImpl extends HibernateDaoSupport implements PersonDAO {
    
      public Person getPerson(String pEmailAddress) throws DataAccessException {
        Person person = null;
        final String query = "from Person as person where " +
          "person.email_address = ?";
        Object param = new Object();
        param = pEmailAddress;
    
        List tmpList = getHibernateTemplate().find(query, param);
    
    
        if &#40;tmpList.size&#40;&#41; < 1&#41; &#123;
          return null;
        &#125;
        person = &#40;Person&#41; tmpList.get&#40;0&#41;; 
    
        // getHibernateTemplate&#40;&#41;.initialize&#40;person.getAddresses&#40;&#41;&#41;;
    
        return person;
      &#125;
    When you execute getPerson([email protected]) is executed.
    A Hibernate Session is opened and the data is read from the database and
    the Person object gets populated. If we do not call this code
    getHibernateTemplate().initialize(person.getAddres ses())
    then the
    collection is not going to be loaded. Which is an expected behavior. I also
    verified from the logs that the Hibernate Session got closed.

    Now let’s come to the actual issue.

    I load the person object for a given email address and store it in the
    HttpSession object as “userSession” and send it to the view. In the view
    there is a link, which would allow you to go to “update account page”.

    When you click on it. It is populating all the person object details.
    Including address details, since the address is not loaded yet (because of
    the lazy=”true”) it is trying to load it now. But I don’t have a reference to
    the original Hibernate Session which loaded this person object. So I get
    the following error

    Code:
    net.sf.hibernate.LazyInitializationException&#58; Failed to lazily initialize a collection - no session or session was closed
    	net.sf.hibernate.collection.PersistentCollection.initialize&#40;PersistentCollection.java&#58;209&#41;
    	net.sf.hibernate.collection.PersistentCollection.read&#40;PersistentCollection.java&#58;71&#41;
    	net.sf.hibernate.collection.Set.iterator&#40;Set.java&#58;130&#41;
    	org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator&#40;ForEachSupport.java&#58;329&#41;
    	org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator&#40;ForEachSupport.java&#58;205&#41;
    	org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare&#40;ForEachSupport.java&#58;137&#41;
    Now my question is what do I do to fix this problem?

    Before addressing to the forum, I tried using the suggestion mentioned by
    Karl Baum Weblog
    http://www.jroller.com/comments/kbau...ation_with_dao

    which did not work for me. Unless I missed something.

    Please help.
    Thanks,
    Arun

  • #2
    You can reconnect the User to a Hibernate Session and initialize the adresses collection:
    Code:
    getSession&#40;&#41;.lock&#40;user, net.sf.hibernate.LockMode.UPGRADE&#41;;
    getHibernateTemplate&#40;&#41;.initialize&#40;person.getAddresses&#40;&#41;&#41;;
    HTH

    Comment


    • #3
      Originally posted by irbouho
      You can reconnect the User to a Hibernate Session and initialize the adresses collection:
      Code:
      getSession&#40;&#41;.lock&#40;user, net.sf.hibernate.LockMode.UPGRADE&#41;;
      getHibernateTemplate&#40;&#41;.initialize&#40;person.getAddresses&#40;&#41;&#41;;
      HTH
      Hmm. Funny I can't see an initialize method on HibernateTemplate nor a lock method on Session. What version of hibernate and spring are you using?

      Comment


      • #4
        I am using Spring ver 1.1.2

        Code:
        getSession&#40;&#41;.lock&#40;user, net.sf.hibernate.LockMode.UPGRADE&#41;;
        getHibernateTemplate&#40;&#41;.initialize&#40;person.getAddresses&#40;&#41;&#41;;
        Just wondering were this code should go. I get the exception in the JSP page which is trying to iterate the collection for display.

        Thanks,
        Arun

        Comment


        • #5
          I've ran into the same problem a number of times and solved it in the following ways:

          - When saving a hibernate object in the session and then later on doing things with it, you have to retrieve it again so that the actual object is the one that hibernate has just retrieved. So I usually just store the userId in the session and then retrieve it upon each new request,
          - or I create an object thats not connected to hibernate but has all the characteristics as the actual object

          Probably not the best way to handle this, but saves me a lot of "ARRRGHHH" remarks while working.

          HTH

          Comment


          • #6
            Originally posted by arun
            I am using Spring ver 1.1.2

            Code:
            getSession&#40;&#41;.lock&#40;user, net.sf.hibernate.LockMode.UPGRADE&#41;;
            getHibernateTemplate&#40;&#41;.initialize&#40;person.getAddresses&#40;&#41;&#41;;
            Just wondering were this code should go. I get the exception in the JSP page which is trying to iterate the collection for display.

            Thanks,
            Arun
            I would say in your DAO: that's where I intend putting it: when I upgrade to spring 1.1.2 that is :-)

            Comment

            Working...
            X