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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:39:29+00:00 2026-05-25T01:39:29+00:00

I am struggling to create a query using the Criteria API. The following SQL

  • 0

I am struggling to create a query using the Criteria API. The following SQL query returns the result that I need:

SELECT * FROM MODEL WHERE MANUFACTURER_ID = 1 AND SHORTNAME LIKE '%SF%' OR LONGNAME LIKE '%SF%';

I have written the following code using the criteria API:

public List<Model> findAllByManufacturer(Manufacturer manufacturer,
        String pattern) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Model> cq = cb.createQuery(Model.class);
    Root<Model> m = cq.from(Model.class);
    cq.select(m);
    Join<Model, Manufacturer> mf = m.join("manufacturer");
    Predicate p = cb.equal(mf.get("id"), manufacturer.getId());
    p = cb.and(cb.like(cb.upper(m.<String>get("shortName")),
            pattern.toUpperCase()));
    p = cb.or(cb.equal(cb.upper(m.<String>get("longName")),
            pattern.toUpperCase()));
    cq.where(p);
    return em.createQuery(cq).getResultList();
}

When I think about it logically, it should work:

Predicate p = cb.equal(mf.get("id"), manufacturer.getId());

WHERE MANUFACTURER_ID = 1

p = cb.and(cb.like(cb.upper(m.<String>get("shortName")),
                pattern.toUpperCase()));

AND SHORTNAME LIKE ‘%SF%’

p = cb.or(cb.equal(cb.upper(m.<String>get("longName")),
                pattern.toUpperCase()));

OR LONGNAME LIKE ‘%SF%’

The query does properly run in that I am not getting any errors, it just is not returning any results. Any clue on where I went wrong are very much appreciated!

  • 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-25T01:39:30+00:00Added an answer on May 25, 2026 at 1:39 am

    Have you checked the generated SQL?

    You’re building a predicate and assigning it to p, but then you’re creating another one and assign it to the same p variable.

    I think it should rather be:

    Predicate predicateOnManufacturerId = cb.equal(mf.get("id"), 
                                                   manufacturer.getId());
    Predicate predicateOnShortName = cb.like(cb.upper(m.<String>get("shortName")),
                                             pattern.toUpperCase()));
    Predicate predicateOnLongName = cb.equal(cb.upper(m.<String>get("longName")),
                                             pattern.toUpperCase()));
    Predicate p = cb.or(cb.and(predicateOnManufacturerId, 
                               predicateOnShortName),
                        predicateOnLongName);
    cq.where(p);
    

    Also, why don’t you use the meta-model of your entities:

    Join<Model, Manufacturer> mf = m.join(Model_.manufacturer);
    ... cb.equal(mf.get(Manufacturer_.id);
    ...
    

    It would be more type-safe, and you would detect incorrect queries at compilation time rather than runtime.

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

Sidebar

Related Questions

I'm struggling with creating a SQL query involving aggregates using PostgreSQL. Consider the following
I've been struggling to create an Oracle SQL query that will tell me if
I am currently struggling with a query that needs to retrieve multiple records from
I'm struggling to create a table view controller that has anything more than just
I'm struggling to create a custom UITableViewCell (in IB) that has a UIImageView on
I'm using mysql to create an hotel booking system, but i am struggling a
I am struggling to find a working query in SQLite, that will return my
I have a mock up of a sql query that will represent a real
I'm struggling with a join. Basically I want the tags results from my query
I am struggling with following query which updates total_books for each category UPDATE book_categories

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.