Hi,
I'm trying to create a Criteria query which the SQL equivalent would be :
Code:
SELECT f2.* from fonctionnalite f2
join (
SELECT distinct f.id from fonctionnalite f
join lien_processus_applicatif_Fonctionnalite l on l.id_fonctionnalite = f.id
join processus_applicatif p on l.id_processus_applicatif = p.id
where p.id_version_applicative = ?
) f1 on f1.id = f2.id;
So far, I managed to obtain this hibernate query :
Code:
select * from fonctionnalite this_
where this_.ID in (
select distinct this_.ID as y0_ from fonctionnalite this_
inner join lien_processus_applicatif_fonctionnalite processusa3_ on this_.ID=processusa3_.ID_FONCTIONNALITE
inner join processus_applicatif paalias1_ on processusa3_.ID_PROCESSUS_APPLICATIF=paalias1_.ID
where paalias1_.ID_VERSION_APPLICATIVE=?
) limit ?
using this criteria :
Code:
DetachedCriteria subCriteria = createDetachedCriteria();
//select distinct f.id from fonctionnalite f
subCriteria.setProjection(Projections.distinct(Projections.id()));
/* JOIN lien l ON l.id_fonctionnalite = f.id
* JOIN pa p ON l.id_pa = p.id
* JOIN va v ON p.id_va = v.id
* WHERE v.id = va
*/
subCriteria.createAlias("processusApplicatifs", "paAlias")
.add(Restrictions.eq("paAlias.versionApplicative.id", va));
Criteria c = createCriteria();
//select f2.* from fonctionnalite f2 WHERE f2.id IN (subCriteria)
c.add(Subqueries.propertyIn("id", subCriteria));
//TODO : find how to do a join instead of a IN statement like this :
//select f2.* from fonctionnalite f2 JOIN (subCriteria) f1 ON f1.id = f2.id;
return c;
What I really want to avoid (if this is possible) is to use the IN statement, but I can't find how to do the join on Subquery with the Criteria API. I tried to use some API like
c.createAlias(associationPath, alias, joinType, withClause) but I don't really understand how to use it :(
Does anybody has an idea ?