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

  • Home
  • SEARCH
  • 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 4600222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:39:12+00:00 2026-05-21T23:39:12+00:00

I am currently working on a project to transfer some legacy jdbc select statements

  • 0

I am currently working on a project to transfer some legacy jdbc select statements over to using Hibernate and it’s criteria api.

The two relevant table columns and the SQL query looks like:

-QUERIES-
primaryId

-QUERYDETAILS-
primaryId
linkedQueryId -> Foreign key references queries.primaryId
value1
value2

select * 
from queries q 
where q.primaryId not in (SELECT qd.linkedQueryId 
                          FROM querydetails qd 
                          WHERE (qd.value1 LIKE 'PROMPT%' 
                                 OR qd.value2 LIKE 'PROMPT%'));

My entity relationships look like:

@Table("queries")
public class QueryEntity{

    @Id
    @Column
    private Long primaryId;
    @OneToMany(targetEntity = QueryDetailEntity.class, mappedBy = "query", fetch = FetchType.EAGER)
    private Set<QueryDetailEntities> queryDetails;

    //..getters/setters..

}

@Entity
@Table(name = "queryDetails")
public class QueryDetailEntity {
    @Id
    @Column
    private Long primaryId;

    @ManyToOne(targetEntity = QueryEntity.class)
    private QueryEntity query;

@Column(name="value1")
private String value1;

@Column(name="value2")
private String value2;

    //..getters/setters..

}

I am attempting to utilize the criteria api in this way:

Criteria crit = sessionFactory.getCurrentSession().createCriteria(QueryEntity.class);

DetachedCriteria subQuery = DetachedCriteria.forClass(QueryDetailEntity.class);
LogicalExpression hasPrompt = Restrictions.or(Restrictions.ilike("value1", "PROMPT%"),
        Restrictions.ilike("value2", "PROMPT%"));
subQuery.add(hasPrompt);
Criterion subQueryCrit = Subqueries.notIn("queryDetails", subQuery);
crit.add(subQueryCrit);

List<QueryMainEntity> entities = (List<QueryMainEntity>) crit.list();
System.out.println("# of results = " + entities.size());

I am getting a NullPointerException on the crit.list() line that looks like

Exception in thread "main" java.lang.NullPointerException
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:362)
    at org.hibernate.criterion.SubqueryExpression.createAndSetInnerQuery(SubqueryExpression.java:153)
    at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:69)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380)
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:114)
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:83)
    at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1687)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)

Now, I think its pretty safe to say I’m using the Criteria Api/Detached Query Api incorrectly, but I’m not sure what the ‘correct’ way to do it is since the Hibernate Docs only briefly cover criteria api subqueries.

I realize this is a pretty long question, but I figure its appear to put it all the relevant aspects of the question (query I’m attempting to represent via Criteria API, tables, entities).

  • 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-05-21T23:39:13+00:00Added an answer on May 21, 2026 at 11:39 pm

    Give this a shot:

    DetachedCriteria d = DetachedCriteria.forClass(QueryDetailEntity.class, "qd");
    d.setProjection(Projections.projectionList().add(Projections.property("qd.query")));
    d.add(Restrictions.or(Restrictions.like("qd.value1", "PROMPT%"), Restrictions.like("qd.value2", "PROMPT%")));
    
    criteria = session.createCriteria(QueryEntity.class, "q");
    criteria.add(Subqueries.propertyNotIn("q.primaryId", d));
    criteria.list();
    

    The use of the following are property names, not column names:

    qd.query
    qd.value1
    qd.value2
    q.primaryId
    

    As a side note, if this is not a dynamically generated query, have you given thought to using HQL instead?

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

Sidebar

Related Questions

I'm currently working on project with Haskell, and have found myself some trouble. I'm
Hi I'm currently working on some php - zend framework project on my osx
I'm curently working on a project where I need to transfer data (with some
I'm currently working on a project where I need to transfer objects from ruby
I'm currently working on a project where I need to transfer objects from ruby
I am currently working on a project and my goal is to locate text
I am currently working on a project with specific requirements. A brief overview of
I am currently working on a project that is moving from .NET 2.0 to
I am currently working on a project where a programmer who fancied themselves a
I am currently working in a project with developers working on three sites. One

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.