Hibernate version:3.0alpha
Name and version of the database you are using:mysql 4.1
Hi,
I have a problem with the criteria api. I am usign .
I have Mail, Receiver classes. Mail has the following one-to-many collection of Receivers:
<set name="receivers"
cascade="none"
lazy="false"
outer-join="true"
inverse="true">
<key>
<column name="mid" not-null="true"/>
</key>
<one-to-many class="Receiver"/>
</set>
My code looks like this:
Criterion crit = ...;
//set eager fetching strategies
crit.setFetchMode("receivers", FetchMode.EAGER);
//add receiver constraint if exists
Criterion reccriterion = ...
if (reccriterion!=null){
Criteria recCriteria = crit.createCriteria("receivers");
recCriteria.add( reccriterion );
}
That is, I always want Receivers to be eagerly fetch, and sometimes I add an additional criterion on receiver. The problem is that I get different sql executed depending wether the recCriteria was added or not.
1.
If it was not, I get the following:
Hibernate: select this_.mid as mid1_, this_.sender as sender1_1_, this_.cc as cc1_1_, this_.senttime as senttime1_1_, this_.subject as subject1_1_, this_.msize as msize1_1_, this_.spam as spam1_1_, this_.togroup as togroup1_1_, this_.toall as toall1_1_, this_.internalsender as interna10_1_1_, this_.lid as lid1_1_, receivers1_.mid as mid__, receivers1_.pk as pk__, receivers1_.pk as pk0_, receivers1_.remail as remail3_0_, receivers1_.rtype as rtype3_0_, receivers1_.cc as cc3_0_, receivers1_.rtime as rtime3_0_, receivers1_.internalreceiver as internal6_3_0_, receivers1_.mid as mid3_0_ from Mail this_ left outer join Receiver receivers1_ on this_.mid=receivers1_.mid where (((this_.senttime<? and this_.senttime>?) and (not (this_.sender like ?) and not (this_.sender like ?))) and this_.sender=?) order by this_.senttime asc
That is, they are joint with an from Mail this_ left outer join Receiver
2.
If it was set, I get:
Hibernate: select this_.mid as mid1_, this_.sender as sender1_1_, this_.cc as cc1_1_, this_.senttime as senttime1_1_, this_.subject as subject1_1_, this_.msize as msize1_1_, this_.spam as spam1_1_, this_.togroup as togroup1_1_, this_.toall as toall1_1_, this_.internalsender as interna10_1_1_, this_.lid as lid1_1_, x0__.pk as pk0_, x0__.remail as remail3_0_, x0__.rtype as rtype3_0_, x0__.cc as cc3_0_, x0__.rtime as rtime3_0_, x0__.internalreceiver as internal6_3_0_, x0__.mid as mid3_0_ from Mail this_ inner join Receiver x0__ on this_.mid=x0__.mid where (((this_.senttime<? and this_.senttime>?) and (not (this_.sender like ?) and not (this_.sender like ?))) and this_.sender=?) and (not (x0__.remail like ?) and not (x0__.remail like ?)) order by this_.senttime asc
Hibernate: select receivers0_.mid as mid__, receivers0_.pk as pk__, receivers0_.pk as pk0_, receivers0_.remail as remail3_0_, receivers0_.rtype as rtype3_0_, receivers0_.cc as cc3_0_, receivers0_.rtime as rtime3_0_, receivers0_.internalreceiver as internal6_3_0_, receivers0_.mid as mid3_0_ from Receiver receivers0_ where receivers0_.mid=?
They are joint with from Mail this_ inner join Receiver, and I get an additional sql to get the receivers.
Is that correct? And is there a way to use always only one sql like in 1?
thanks
|