I'm using detachedCriteria to create a correlated subquery. This all works fine until I try to add an alias to the detachedcriteria in which case hibernate adds the restriction, but does not add a join for the new aliased table. Unfortunately hibernate also don't throw any kind of exception to let me know what I'm doing wrong. Could someone please point me in the right direction
Code:
DetachedCriteria subCriteria = DetachedCriteria.forClass(persistenceMapping.getClassToSave(),detachedAlias)
.createAlias(detachedAlias + "." + "eventType", "ET1")
.setProjection( Property.forName(detachedAlias + "." + attribute).max())
.add( Property.forName(detachedAlias + "." + matchAttribute).eqProperty(mainCriteriaAlias + "." + matchAttribute) )
.add(Restrictions.eq("ET1.eventTypeCode", "OPEN"));
Criteria criteria = session.createCriteria(persistenceMapping.getClassToSave(),mainCriteriaAlias);
criteria.add(Subqueries.propertyEq(attribute, subCriteria));
Map aliasMap = new HashMap();
hibernateReflectionUtil.generateUniqueRestrictionsByVo(
session, criteria, persistenceMapping.getClassToSave(), restriction, false, true, aliasMap);
if (join!=null)
hibernateReflectionUtil.generateJoinRestrictionsByVo(session, persistenceMapping.getClassToSave(), join, criteria, false);
String excludeAttName = excludeAttrib;
int indexOf =excludeAttrib.indexOf(".");
if (indexOf>-1){
excludeAttName = (String) aliasMap.get(excludeAttrib.substring(0,indexOf));
excludeAttName += "." + excludeAttrib.substring(indexOf+1);
}
criteria.add(Restrictions.not(Restrictions.in(excludeAttName, excludeList)));
list = criteria.list();
resultant sql
Code:
select
this_.ACC_EVENT_ID as ACC1_4_5_,
this_.ROW_TOUCHED_DATETIME as ROW2_4_5_,
this_.EVENT_PTY_ID as EVENT3_4_5_,
this_.ACC_ID as ACC4_4_5_,
this_.ACTUAL_EVENT_DT as ACTUAL5_4_5_,
this_.ACTUAL_EVENT_DT_GRANULARITY as ACTUAL6_4_5_,
this_.EXPECTED_EVENT_DT as EXPECTED7_4_5_,
this_.EXPECTED_EVENT_DT_GRANULARITY as EXPECTED8_4_5_,
this_.EVENT_TYPE_ID as EVENT9_4_5_,
alias3x1_.EVENT_TYPE_ID as EVENT1_14_3_,
alias3x1_.EVENT_TYPE_CODE as EVENT2_14_3_,
alias3x1_.EVENT_TYPE_NAME as EVENT3_14_3_,
alias3x1_.EVENT_TYPE_DESC as EVENT4_14_3_,
alias3x1_.ROW_STATUS_IND as ROW5_14_3_,
alias3x1_.SCHEME_ID as SCHEME6_14_3_,
alias4x2_.SCHEME_ID as SCHEME1_11_4_,
alias4x2_.PARENT_SCHEME_ID as PARENT2_11_4_,
alias4x2_.IMMUTABLE_MEMBERS_FLAG as IMMUTABLE3_11_4_,
alias4x2_.MULT_MEMBERS_ALLOWED_FLAG as MULT4_11_4_,
alias4x2_.SCHEME_DESC as SCHEME5_11_4_,
alias4x2_.SCHEME_NAME as SCHEME6_11_4_,
alias4x2_.SCHEME_CODE as SCHEME7_11_4_
from
ACCOUNT_EVENT this_
inner join
EVENT_TYPE alias3x1_
on this_.EVENT_TYPE_ID=alias3x1_.EVENT_TYPE_ID
inner join
SCHEME alias4x2_
on alias3x1_.SCHEME_ID=alias4x2_.SCHEME_ID
where
this_.ACTUAL_EVENT_DT = (
select
max(this0__.ACTUAL_EVENT_DT) as y0_
from
ACCOUNT_EVENT this0__
where
this0__.ACC_ID=this_.ACC_ID
and et1x1_.EVENT_TYPE_CODE=?
)
and alias4x2_.SCHEME_ID=?
and not alias3x1_.EVENT_TYPE_CODE in (
?, ?, ?
)