java - Eagerly fetch child when loading parent -


i use spring data jpa , hibernate provider.

i have parent class mapped follows:

@entity @table(name="parent") public class parent {      private list<child> childs;     private list<anotherchild> anotherchilds;      @onetomany(mappedby = "parent", fetch = fetchtype.eager)     public list<child> getchilds() {         return childs;     }      @onetomany(mappedby = "parent", fetch = fetchtype.lazy)     public list<anotherchild> getantoherchilds() {         return anotherchilds;     }  } 

and child:

@entity @table(name="child") public class child {      private parent parent;      @manytoone(fetch = fetchtype.eager)     @joincolumn(name = "column_name")     public parent getparent() {         return patern;     }  }  @entity @table(name="another_child") public class anotherchild {      private parent parent;      @manytoone(fetch = fetchtype.eager)     @joincolumn(name = "column_name")     public parent getparent() {         return patern;     }  } 
  • when load parent database, doesn't load list of child ,
  • when call parent.getchilds(), returns null.

can give advice? wrong anywhere? thanks.

edit:

after research, realize when have single child, loaded eagerly (like should). when have multiple child, doesn't - though has been marked fetchtype.eager , other fetchtype.lazy.

note: if marked both fetchtype.eager, it'll throws multiplebagfetchexception: cannot simultaneously fetch multiple bags.

the same happened when annotate using @fetch(fetchmode.join)

if added entity annotation parent.getchilds() should not come empty.it better entity.

    @entity      @table(name="parent_tbl")     public class parent {         //other fields         @onetomany(mappedby = "parent",fetch = fetchtype.lazy,cascade = {cascadetype.merge, cascadetype.persist, cascadetype.remove},orphanremoval = true)         private list<child> childs;         //getter setter             }       @entity      @table(name="child_tbl")     public class child {      //other fields       @manytoone(fetch = fetchtype.lazy)       @joincolumn(name = "parent_id")       private parent parent;     //getter setter   } 

example parent query;

public parent getparent(long parentid) throws exception {    session = sessionfactory.opensession();        criteria cr = session.createcriteria(parent.class, "parent");         cr.setfetchmode('parent.childs', fetchmode.join);         cr.add( restrictions.eq("parent.id", parentid));         parent parent = cr.uniqueresult();         tx = session.gettransaction();         session.begintransaction();         tx.commit();     return parent; 

eager loading of collections means fetched @ time parent fetched. if have parent , has list, childs fetched database @ time parent fetched.

lazy on other hand means contents of list fetched when try access them. example, calling parent.getchilds().iterator(). calling access method on list initiate call database retrieve elements. implemented creating proxy around list (or set). lazy collections, concrete types not arraylist , hashset.


Comments