Hibernate Criteria: Perform JOIN in Subquery/DetachedCriteria
- by Gilean
I'm running into an issue with adding JOIN's to a subquery using DetachedCriteria. The code looks roughly like this:
Criteria criteria = createCacheableCriteria(ProductLine.class, "productLine");
criteria.add(Expression.eq("productLine.active", "Y"));
DetachedCriteria subCriteria = DetachedCriteria.forClass(Models.class, "model");
subCriteria.setProjection(Projections.rowCount());
subCriteria.createAlias("model.language", "modelLang");
criteria.add(Expression.eq("modelLang.language_code", "EN"));
subCriteria.add(Restrictions.eqProperty("model.productLine.id","productLine.id"));
criteria.add(Subqueries.lt(0, subCriteria));
But the logged SQL does not contain the JOIN in the subquery, but does include the alias which is throwing an error
SELECT *
FROM PRODUCT_LINES this_
WHERE this_.ACTIVE=?
AND ? <
(SELECT COUNT(*) AS y0_
FROM MODELS this0__
WHERE modelLang3_.LANGUAGE ='EN'
AND this0__.PRODUCT_LINE_ID =this_.ID
)
How can I add the joins to the DetachedCriteria?
Hibernate version: 3.2.6.ga
Hibernate core: 3.3.2.GA
Hibernate annotations: 3.4.0.GA
Hibernate commons-annotations: 3.3.0.ga
Hibernate entitymanager: 3.4.0.GA
Hibernate validator: 3.1.0.GA