Hibernate Criteria: Perform JOIN in Subquery/DetachedCriteria
Posted
by Gilean
on Stack Overflow
See other posts from Stack Overflow
or by Gilean
Published on 2010-04-08T18:34:35Z
Indexed on
2010/04/09
3:03 UTC
Read the original article
Hit count: 1264
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
© Stack Overflow or respective owner