4 Replies Latest reply on Jan 16, 2006 5:57 AM by cyril.joui

    FetchType.LAZY

    danjourno

      I have an entity called commission that has a property of supplier with the reference to this object set up as below

       @ManyToOne(fetch=FetchType.LAZY)
       @JoinColumn(nullable=false, name="supplier_id")
       public Supplier getSupplier() {
       return supplier;
       }


      I also have a Stateless session bean called PersistenceManager that as you probably guessed manages all the persistence operations for my entity beans.

      Then I have another sesison bean that uses this PersistenceManager to retrieve an instance of the Commission object.

      when I try to get the supplier object attached to this Commission object with

      commission.getSupplier();

      I get the following..

      org.hibernate.LazyInitializationException: could not initialize proxy - no Session


      Any ideas as to why I cannot "get" an attached entity that has a fetch type of LAZY?

      Regards
      Dan

        • 1. Re: FetchType.LAZY
          ejb3workshop

          Are you calling commission.getSupplier(); on the client side or within the session bean ?

          If you are calling it on the client side you will get a LazyException as you are trying to access an object outside of it's persistent context. In this case to fix the problem you will need to either:
          1.) fetch the associated objects within the session bean and return both to the client
          2.) Set the type to EAGER if the two objects are closely related. (Composition Relationship ???)

          I hope this helps. If you have further input please send it.

          Alex
          ejb3workshop.com
          Video Podcasts on EJB3

          • 2. Re: FetchType.LAZY

            For more informations you can look at :
            http://jboss.com/index.html?module=bb&op=viewtopic&t=75525

            Good luck

            • 3. Re: FetchType.LAZY
              pvanonselen

              Check persistence.xml under JBOSS_HOME/server/all/ejb3.deployer/META-INF.

              When release_mode is on after_transaction it closes the session after each JDBC statement.

              Resolve problem by uising on_close or just comment the line containing the following line

              hibernate.connection.release_mode=after_statement


              See Chapter 4 of Hibernate 3 reference guide 4.Configuration for more details

              • 4. Re: FetchType.LAZY

                Hello,

                I think, you can overload it in your persistence.xml of your .par file