title: Subquery function in Hibernate
body: <p>I am trying to use subquery function in my Hibernate query, but I'am an idiot :) Structure of my DB tables is: </p>
  
  <pre><code>CREATE TABLE standard (
    id VARCHAR(12) NOT NULL ,
    title VARCHAR(99) NOT NULL ,
    description VARCHAR(999) NOT NULL ,
    revision VARCHAR(99) NOT NULL ,
    annotation TEXT NULL ,
    PRIMARY KEY (id) );
  
  CREATE TABLE article (
    id VARCHAR(12) NOT NULL ,
    type INT(2) NOT NULL ,
    classTitle VARCHAR(99) NULL ,
    classDescription VARCHAR(999) NULL ,
    standard_id VARCHAR(12) NULL ,
    pubdate DATETIME NOT NULL ,
    title VARCHAR(99) NULL ,
    thumbnail TEXT NULL ,
    text TEXT NULL ,  
    PRIMARY KEY (id) );
  </code></pre>
  
  <p>What I'm going to do is to get standard, which contains articles with specified type. My thought was that I filter articles by its type, then group them by its standardId and then get standards through its ids.</p>
  
  <p>Something like this :)</p>
  
  <pre><code>DetachedCriteria subquery = DetachedCriteria.forClass(Article.class)
                               .add(Restrictions.eq("type", Constants.ARTICLE_TYPE_INTERPRETATION))
                                .setProjection(Projections.groupProperty("standardId"));
  
  List&lt;Standard&gt; stds = (List&lt;Standard&gt;) session.createCriteria(Standard.class)
              .add(Restrictions.idEq(Subqueries.exists(subquery))).list();
  </code></pre>
  
  <p>But this query gives me all of the standards in database despite the fact, that subquery returns only standard that I want. I know where the problem is : I can't write the Restriction that will filter standard by its ids returned by subqery.</p>
  
  <p>Can someone give me a hint how to do it ? </p>
  
  <p>Many thanks !</p>
  
  <p>Ondrej</p>
  
  <p><strong>EDIT:</strong>
  Thanks to @Rahul Agrawal</p>
  
  <pre><code>List&lt;Standard&gt; stds = (List&lt;Standard&gt;) session.createCriteria(Standard.class)
              .add(Subqueries.propertyIn("id", subquery)).list();
  </code></pre>