Hi all,
Does anyone knows how to change the order a Criteria generates its JOINs? Let me use an example.
I have this criteria (with DetachedCriteria modified by me to accept the withClause in the join):
Code:
DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
criteria.createCriteria("pc.protocol", "pcp");
criteria.createCriteria("protocolCustomer", "pc");
criteria.createCriteria("group", "gr");
criteria.createCriteria("gr.protocolGroupSet", "pgs", Criteria.INNER_JOIN, Restrictions.eqProperty("protocolGroupPK.protocolId", "pcp.protocolId"));
This criteria generates this SQL code:
Code:
select
*
from
CUSTOMERS this_
inner join
COMPANY_GROUPS gr3_
on this_.GROUP_ID=gr3_.GROUP_ID
inner join
PROTOCOL_GROUP pgs4_
on gr3_.GROUP_ID=pgs4_.GROUP_ID
and (
pgs4_.PROTOCOL_ID=pcp1_.PROTOCOL_ID
)
inner join
PROTOCOL_CUSTOMER pc2_
on this_.CUSTOMER_ID=pc2_.CUSTOMER_ID
inner join
PROTOCOL pcp1_
on pc2_.PROTOCOL_ID=pcp1_.PROTOCOL_ID
and this throws a SQL Error because this line: pgs4_.PROTOCOL_ID=pcp1_.PROTOCOL_ID. This is completely normal because at the moment this query is trying to be executed, pcp1_ alias has not yet been defined. This would be resolved if I can change the inner join with PROTOCOL_GROUP to the last position, after the inner join with PROTOCOL.
Can i modify the order in which the JOINs are executed within a Criteria?
Thank you.