These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: LAZY AND EAGER FETCHES - EXTREME CASE
PostPosted: Thu Oct 29, 2009 11:00 am 
Newbie

Joined: Thu Oct 29, 2009 10:16 am
Posts: 1
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/


Top
 Profile  
 
 Post subject: Re: LAZY AND EAGER FETCHES - EXTREME CASE
PostPosted: Fri Oct 30, 2009 3:22 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
This is by design. Search for CriteriaUtil.DistinctRootEntity, there should be a lot of posts discussing that. In your case you can solve the problem if you switch from bag to set (which only allow one instance of each object).

Hibernate returns three objects because of the joi returning 3 rows, but only one instance of the object is created. The 3 items in your list are all the same object.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.