8 Replies Latest reply on Mar 4, 2006 4:50 PM by treespace

    When to use EAGER / LAZY

    Gus Gu Expert

      Hi all,

      I currently have a quesion about using the EAGER/LAZY. I have a class:

      public class Folder implements Serializable {
       @OneToMany (mappedBy="folder", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
       public List<File> getFiles() {
       return files;

      After I load a folder from client, then use getFiles() to get all files, I got exception:
      org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Folder.files, no session or session was closed

      However, when I changed the LAZY to EAGER, no exception happened. But, I really do not want to client to load all files before it calls the getFiles(). How should I do? Any help will be appreciate. Thank you in advance. I use EJB RC5 and JBoss 4.0.4

        • 1. Re: When to use EAGER / LAZY

          Did you call getFiles() in your Session Bean before calling the method on the entity in the client?

          • 2. Re: When to use EAGER / LAZY
            ppc Newbie

            if you are using the remote interface when the bean is serialized the field files is null and jboss does not initialize the field because you ask to be lazy, peraphs if you client lives in the container using @local interface the things must work

            • 3. Re: When to use EAGER / LAZY
              Gus Gu Expert

              Thank you all for your response. What I did is:

              First, I use JSF session bean to call local interface of dao stateless bean to get a folder instance using a EJBQL, then call the folder.getFiles() in the JSF session bean to try to get all files under the folder. If I set fetch=FetchType.LAZY, I would get exception shown above. If I set fetch=FetchType.EAGER, it will work. I do not know why I got the exception on the LAZY. Thanks for your help.

              • 4. Re: When to use EAGER / LAZY
                Nic Holbrook Newbie

                The session is opened and closed inside the stateless bean method. Once the folder object is passed back across the wire, it is no longer connected to the session / datasource and cannot retrieve the files. You might consider writing a getFiles method in your stateless bean and call that instead of the getFiles on your Folder object if you want it to be lazy and detached.

                • 5. Re: When to use EAGER / LAZY
                  Gus Gu Expert

                  Hi, thank you very much for your response.

                  "Writing a getFiles method in your stateless bean" means to create another EJBQL to retrieve files using the passed folder object. Is it correct? Thanks.

                  • 6. Re: When to use EAGER / LAZY
                    Nic Holbrook Newbie

                    Either that or make your fetch type eager.

                    • 7. Re: When to use EAGER / LAZY
                      Gus Gu Expert

                      That's great! I got it. Thank you so much.

                      • 8. Re: When to use EAGER / LAZY
                        treespace Novice

                        This is a fundamental question of paramount importance to EJB 3.0 entity bean development. EJB 3.0 keeps it simple and loads everything. You can take a different tack and lazy load everything for maximum performance.

                        When you get an exception such as yours, you can analyze the situation. If that item is accessed with high frequency then mark it as eager. If access to that information is infrequent, mark it as lazy and add a session bean method to explicitly populate those fields from its entity manager.

                        Having default lazy loading with magical remote loading would be nice but what entity manager would it use? Also, you would then have to remote the business logic that may or may not be applied at load time.

                        It would be useful if an official WHEN-TO was published for lazy/eager but I think I am on the right track with my comments. Just starting to use EJB 3.0 myself.