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 6635537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:04:02+00:00 2026-05-25T23:04:02+00:00

Suppose that I have a JPA-annotated class called MyData with a primary identifier field

  • 0

Suppose that I have a JPA-annotated class called MyData with a primary identifier field (BigDecimal type called “primaryID”), and two Boolean fields called “fieldA” and “fieldB”. What I want to do is create a JPA query that will select fieldA AND fieldB for a given MyData instance.

        final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        final CriteriaQuery<Boolean> boolQuery = builder.createQuery(Boolean.class);
        final Root<MyData> data = boolQuery.from(MyData.class);
        boolQuery.select(builder.isTrue(builder.and(data.<Boolean> get("fieldA"),
                profile.<Boolean> get("fieldB"))));
        final Predicate primaryIDPredicate = builder.equal(profile.<BigDecimal> get("primaryID"),
                1000);
        boolQuery.where(primaryIDPredicate);

        final TypedQuery<Boolean> myQuery = entityManager.createQuery(boolQuery);

When my entityManager executes this query, I get: org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: and near line 1.... This leads me to believe that I need something different (other than the builder.isTrue method) to designate that I want to take a Boolean and of two fields of my object. Any ideas on how I should construct this query?

  • 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-25T23:04:03+00:00Added an answer on May 25, 2026 at 11:04 pm

    OK, what I ended up doing was simply moving the booleans I wanted to “AND” into the predicate instead, and just checking whether anything was returned. Of course, I could have simply retrieved the entire object (MyData) based on the primary ID, and done the “AND” in Java code, but the point was to avoid fetching the full object because it has a large number of nested joins to its referenced collections, and those would be unnecessary for this case.

            final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
            final CriteriaQuery<BigInteger> primaryIDQuery = builder.createQuery(BigInteger.class);
            final Root<MyData> data = primaryIDQuery.from(MyData.class);
            primaryIDQuery.select(data.<BigInteger> get("primaryId"));
            final Predicate primaryIDPredicate = builder.equal(profile.<BigInteger> get("primaryId"),1000);
            final Predicate otherPredicate = builder.or(
                    builder.isTrue(profile.<Boolean> get("fieldA")),
                    builder.isTrue(profile.<Boolean> get("fieldB")));
            primaryIDQuery.where(primaryIDPredicate , otherPredicate);
    
            final TypedQuery<BigInteger> existenceQuery = entityManager.createQuery(primaryIDQuery);
    
            boolean whatIWant = existenceQuery.getResultList().size() > 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that we have a class called class1. The class1 has a method called
Suppose that I have a table like: class Ticker(Entity): ticker = Field(String(7)) tsdata =
Suppose that I have a class like this class Employee: pass I create two
Suppose that you have two huge files (several GB) that you want to concatenate
Suppose that I have a Java class with a static method, like so: class
Suppose that I have an integer variable in a class, and this variable may
suppose that you have two activities A(which is the main activity) and B when
Suppose that I have a C data-structure containing many data fields (>15): struct MyData
Suppose that I have two data types Foo and Bar. Foo has fields x
Suppose that I have the following record: type t = {a:int} In order to

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.