4 Replies Latest reply on Dec 4, 2008 11:17 AM by nschweig

    Failed to Laizy load the Collection

    efmcuiti

      Hello everyone,

      I'm using jboss 4.0.5 GA and EJB3 specification with JPA(Java Persistence API). In one persistent entity a have something like this:

      @OneToMany
      private Collection roles;

      But when in my web application (in a Servlet or jsp) i do something like this:

      Collection roles = user.getRoles() (in a servlet)
      or
      ${user.roles} (in a JSP),

      The jboss console displays:

      Laizy Initialization failed in collection of role: org.tha.Role (or something like this)

      I don't know if have something related with entity managers or with the database that i'm using (Mysql 4.1).

      Thanks if you people can help me.

        • 1. Re: Failed to Laizy load the Collection
          efmcuiti

          Here is the error message that the jboss console throws:

          org.hibernate.lazyInitializationException: failed to lazily initialize a collection of role: org.tha.User.roles, no session or session was closed

          I hope you can help me

          • 2. Re: Failed to Laizy load the Collection
            reubenf

            I think the problem is as follows: With lazy loading, objects are not loaded until referenced. Until then, references are to proxy objects. So, since all loads need to be done in the context of a session, if you load a parent object and never reference the child object until after the session has been closed, you will get a lazy initialization exception

            • 3. Re: Failed to Laizy load the Collection
              efmcuiti

               

              "reubenf" wrote:
              I think the problem is as follows: With lazy loading, objects are not loaded until referenced. Until then, references are to proxy objects. So, since all loads need to be done in the context of a session, if you load a parent object and never reference the child object until after the session has been closed, you will get a lazy initialization exception


              Yes, i've realized that before.... but there is no way to configure the container in order to work with lazy load without problems?

              Thanks

              • 4. Re: Failed to Laizy load the Collection
                nschweig

                Hi,

                I have got the same problem. In the hibernate forum they say that you have to use the opensessioninview pattern.
                http://www.hibernate.org/43.html

                If you use Spring (I do not) there ist a OpenSessionInViewFilter you can use (I think you only have to configure it in the web.xml)
                But I do not really understand how to implement the OSIV-Pattern by myself when I use EJB.... ???
                Perhaps you have some ideas.

                Nicki