Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8351227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:34:13+00:00 2026-06-09T08:34:13+00:00

I am trying to use subquery function in my Hibernate query, but I’am an

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:34:15+00:00Added an answer on June 9, 2026 at 8:34 am

    Add an IN condition.
    To add a “where field in subquery”, two separate criteria are required. One for the main (root) entity, and the other to retrieve the values for the IN list.
    The following retrives the orders for which it has an order item which the product has a stock level of ZERO. The result transformer is used to only retrieve one row per entity, instead of having multiple rows as a result of the join on a one-to-many relationship.
    ?

    DetachedCriteria ids = DetachedCriteria.forClass(ProductStock.class, "stock");
    ids.add(Restrictions.eq("stock.stockLevel", 0));
    ids.setProjection(Property.forName("productId"));
    
    DetachedCriteria criteria = DetachedCriteria.forClass(Order.class, "order");
    criteria.createAlias("order.orderItems", "items", CriteriaSpecification.LEFT_JOIN);
    criteria.add(Subqueries.propertyIn("items.productId", ids));
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);
    

    For more details refer : http://devgrok.blogspot.in/2008/11/hibernates-criteria-api.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to use parameter from external query in subquery FROM clause. tbl1: | id
I am trying to use the query below (which works perfectly), as a subquery
I am trying use gem tire to search in my application. I have tables
trying to use hibernate with my web app and getting following exception: Initial SessionFactory
I'm trying to get a query to compare two tables by LEFT OUTER JOINing
I'm trying to convert a standard SQL query to a LINQ to SQL query.
I'm trying to use NHibernate filters but I'm obtaining a really weird SQL translation
I'm trying to create a relatively complex MySQL query to generate a stacked bar
I am trying to use group by clause in subquery which is in from
I am trying to use MySQL benchmark to test some queries. But, I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.