2 Replies Latest reply on Nov 27, 2008 5:42 AM by mravikrish

    failed to lazily initialize a collection of role

    mravikrish

      hello,

      I am ramu, i am getting a problem in many to many mapping .my entities are student and courses student--->courses is many to many.

      while retriving courses from student ie

      i am getting the following exception

      failed to lazily initialize a collection of role
      can any body suggest where i am doing mistake.

      Thank you
      KRamu


        • 1. Re: failed to lazily initialize a collection of role
          wolfgangknauf

          Hi Ramu,

          the problem is the "fetchType" attribute on your relationship. It defaults to "FetchType.LAZY", which means that the other side of the relation is not loaded until required. If the entity gets detached and the relationship field is not loaded, you will get this exception on client side.

          To work around you could
          a) set the fetchType to "FetchType.EAGER" (not recommended for ManyToMany)
          b) explicitely force loading the relationship field BEFORE the entity gets detached (before the session bean method is left).
          To do so: call e.g. "student.getCourses().size();" after loading a student.
          Calling only "student.getCourses();" does not help, you have to access the collection content to make it load.

          Hope this helps

          Wolfgang

          • 2. Re: failed to lazily initialize a collection of role
            mravikrish


            Thanks wolfgang it worked