3 Replies Latest reply on Jan 16, 2006 10:33 AM by Cyril JOUI


    Fabrice Clari Newbie

      Hi everybody,

      One more question on that topic, sorry, but I did not find any -positive- solution to my issue.

      I have an entity bean, Employee. Employee has an Address (@OneToOne relation, CascadeType set to All) with FetchType set to Lazy (because when I browse the employees list I do not want to have all the addresses and countries loaded).

      When trying to update the address (through a session bean) on an Employee instance, I got an exception saying: "LazyInitializationException: could not initialize proxy - no session".

      I understood from the previous posts on that topic that this can only be done in a local context; this is my case: a webapp inside JBoss (4.0.3sp1) accesses session beans on the same server (through context.lookup(MyBean.class.getName())). Furthermore, I am looking for a solution with the FetchType set to Lazy.

      Any help will be greatly appreciated since I got lost in the EJB3 Jungle... Tarzan, Jane, are you there ?? ;-)

      Thanks and best regards,

        • 1. Re: FetchType.Lazy
          Cyril JOUI Newbie

          Hello !

          You must explicitly initialize your object before returning it to your webapp !
          You can do this just with calling the size() method of your collection !

          Of course it would be easier if you use the Facade Pattern with a Session Bean !

          for example (in the session bean)

          public Employee findEmployee(int id) {
           Employee employee = employeeDao.findEmployeeById(id);
           // this line initialize address collection
           // be careful if the collection == null !!!

          I hope it would be easier for you ;)

          Good luck

          • 2. Re: FetchType.Lazy
            Fabrice Clari Newbie

            Hi Cyril,

            Many thanks for your answer.
            One precision: do I need to do the same thing in case of a Collection? eg inside the session, I have Collection getAllEmployee(). I tried to make a loop on that collection and initialize the address for each employee but it does not work (but it worked for getEmployeeById()). Is is normal?

            Once again, thanks for your help.

            • 3. Re: FetchType.Lazy
              Cyril JOUI Newbie


              By default, the OneTOne and OneToMany is loaded automaticaly.
              Collections are automaticly in the lazy mode !

              If you have a Collection of User and a User has a Collection of resources and a Resource has a collection of Category (for example).
              You must initialize the collection of resources and for each Resource you have to initialize the collection of Category.

              Do you see ? ;)