2 Replies Latest reply on Oct 9, 2009 10:47 AM by Dmitry Shultz

    Join Fetch doesn't work for empty sets?

    Dmitry Shultz Newbie

      Hello All,


      I want to reduce the db calls amount and decided to refactor my code to load all the related stuff in one query using 'join fetch'.


      The first problem was with Hibernate unable to load List collections (multiple bag's), so I've converted it to HashSet's.


      But seems like it doesn't work 100% for Set's ether, I have something similar to this:




      @Entity
      @Inheritance(strategy = InheritanceType.JOINED)
      public class BaseObj {
      
      @Id
      @GeneratedValue
      private Long id;
      
      
      }
      
      @Entity
      @Name("bigObj")
      public class BigObj extends BaseObj {
      
      
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "big", fetch = FetchType.LAZY)
      private Set<SomeItem> items;
      
      }
      
      
      
      @Entity
      public class SomeItem {
      
      @Id
      @GeneratedValue
      private Long id;
      
      @ManyToOne
      private BigObj big;
      
      } 
      
      



      Following query (the BigObj with id eq 1 exists in the database):


      select distinct o from BigObj join fetch o.items  where o.id = 1



      loads BigObj only if items Set is not empty, if i remove the 'join fetch o.items' the BigObj is loaded.


      Not sure if this is the right place to ask Why?, but probably a lot of seamsters have some experience with the 'fetch join' and somebody has the time to confirm if it supposed to work or not.