Lets say I have an entity , in this entity I got 2 lists that fetches from two other entities :
@Entity @Table (name="SOME_TABLE") @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) public class SomeTableDt { private String name; private Integer value; private Map<String , SomeMapDt> someMap; private List<SomeListDt> SomeList; @Id @Column(name = "name") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name = "value") public Integer getValue() { return value; } public void setValue(Integer value) { this.value = value; } @OneToMany(mappedBy="someTableDt", fetch=FetchType.EAGER) @MapKey(name="someMapId.fileSuffix") @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) public Map<String, SomeMapDt> getSomeMap() { return someMap; } public void setSomeMap(Map<String , SomeMapDt> someMap) { this.someMap = someMap; } @OneToMany(mappedBy="id.fileType", fetch=FetchType.EAGER) public List<SomeListDt> getSomeList() { return someList; } public void setSomeList(List<SomeList> someList) { this.someList = someList; } Now if I use in both cases (someList and someMap) EAGER FETCH , there will be only one select that will fetch the all answer. The problem starts when one list returns one row answer , and the Map returns three row answer. In that case , if I need to use only the list fetch (one answer only), it will be 3 answers instead of one,and they will be exectlly the same , but if I use the LAZY FETCH , it will return one item.
differences between fetches (using hibernate property showsql="true") : before : select someEntitydt0_.name as name66_2_, someEntitydt0_.value as value66_2_, somelist1_.SOME_ENTITY as file1_4_, somelist1_.in_SOME_LIST as in2_4_, somelist1_.out_SOME_LIST as out3_4_, somelist1_.SOME_ENTITY as file1_68_0_, somelist1_.in_SOME_LIST as in2_68_0_, somelist1_.out_SOME_LIST as out3_68_0_, SOME_MAP2_.SOME_ENTITY as file2_5_, SOME_MAP2_.file_suffix as file1_5_, SOME_MAP2_.file_suffix as formula0_5_, SOME_MAP2_.file_suffix as file1_67_1_, SOME_MAP2_.SOME_ENTITY as file2_67_1_, SOME_MAP2_.service_name as service7_67_1_, SOME_MAP2_.split_service_name as split8_67_1_ from ITP_SOME_ENTITY someEntitydt0_ left outer join ITP_SOME_LIST somelist1_ on someEntitydt0_.name=somelist1_.SOME_ENTITY left outer join ITP_REPL_SERVICE_NAME SOME_MAP2_ on someEntitydt0_.name=SOME_MAP2_.SOME_ENTITY where someEntitydt0_.name=?
and after : select someEntitydt0_.name as name66_1_, someEntitydt0_.file_group_enum as file2_66_1_, someEntitydt0_.is_automatic as is3_66_1_, someEntitydt0_.is_create_report_to as is4_66_1_, someEntitydt0_.transaction_type as transact5_66_1_, someEntitydt0_.value as value66_1_, somelist1_.SOME_ENTITY as file1_3_, somelist1_.in_SOME_LIST as in2_3_, somelist1_.out_SOME_LIST as out3_3_, somelist1_.SOME_ENTITY as file1_68_0_, somelist1_.in_SOME_LIST as in2_68_0_, somelist1_.out_SOME_LIST as out3_68_0_ from ITP_SOME_ENTITY someEntitydt0_ left outer join ITP_SOME_LIST somelist1_ on someEntitydt0_.name=somelist1_.SOME_ENTITY where someEntitydt0_.name='makefet'
So is this a common problem in hibernate , or maybe I didsome thing wrong/
|