I am trying to use subquery function in my Hibernate query, but I'am an idiot :) Structure of my DB tables is:
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) );
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.
Something like this :)
DetachedCriteria subquery = DetachedCriteria.forClass(Article.class)
.add(Restrictions.eq("type", Constants.ARTICLE_TYPE_INTERPRETATION))
.setProjection(Projections.groupProperty("standardId"));
List<Standard> stds = (List<Standard>) session.createCriteria(Standard.class)
.add(Restrictions.idEq(Subqueries.exists(subquery))).list();
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.
Can someone give me a hint how to do it ?
Many thanks !
Ondrej
EDIT: Thanks to @Rahul Agrawal
List<Standard> stds = (List<Standard>) session.createCriteria(Standard.class)
.add(Subqueries.propertyIn("id", subquery)).list();