1 Reply Latest reply on Dec 6, 2007 11:04 AM by elnino

    LazyLoading question when accessing to attributes from class

    elnino

      Hello,

      I have set some attributes with LazyLoading property, it's working great. But I have question.

      Take this example

      @OneToMany(mappedBy = "content", cascade = { CascadeType.PERSIST,
       CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH }, fetch=FetchType.LAZY)
      private List<Content> contents;
      
      ....
      with the setter and getter.
      ....
      
      


      If I call getContents() method, the lazy loading is working perfectly and hibernates doing his job.

      Now if I have a method that make a search in contents list... I will have

      
      public void doStuffInContents() {
      
      // Doing stuff on contents attributes...
      
      }
      
      


      Of course, the contents attributes is null...

      The solution can be using getContents() method instead contents attributes directly.

      Anothe one could be :
      if (contents == null)
      contents = getContents();
      

      in each method using the attributes.

      But I don't like these two...

      I would like to get your point of view... Thanks !

      Regards,