-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Join on Subquery (Criteria)
PostPosted: Fri Apr 29, 2011 10:40 am 
Newbie

Joined: Fri Apr 29, 2011 10:29 am
Posts: 5
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 ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.