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

The Archive Base Latest Questions

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

I am writing a JPA QL named query that would search for documents. This

  • 0

I am writing a JPA QL named query that would search for documents. This should be done in only one query and I cannot switch to native SQL (that’s non functional requirement).
The query I wrote looks as follows:

query = "select doc from Doc doc " +
" join doc.Csts cst " +
" where cst.cstFrstNm like :FrstNm " +
" and cst.cstLastNm like :LastNm " +
" and doc.resId = :id ";

This query is parametrized using following instructions:

query.setParameter("FrstNm ", firstName + '%');
query.setParameter("LastNm ", lastName + '%');            
query.setParameter("id", resId);

firstName, lastName, resId in the above code are my search criteria and they are all Strings and they can be null. When one of them is null, it shouldn’t be taken into consideration when the query is invoked.

For instance:

Let’s say that: firstName = “John”, lastName = “Doe” and resId is null, then the query should return all the entries from Doc table that are for user John Doe, no matter what is their resId.

I was trying to put additional OR and AND conditions into query that’d check if resId is null but it didn’t work. I’m testing it on HSQLDB.

Is it any way to modify this JPA query to handle null values?

  • 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-22T23:12:52+00:00Added an answer on May 22, 2026 at 11:12 pm

    Two solutions:

    First one, don’t use a named query and craft a new query for each request:

    boolean first = true;
    query = query = "select doc from Doc doc " +
    " join doc.Csts cst " +
    if (firstName != null) {
        query += (first ? " where " : " and ");
        query += " cst.cstFrstNm like :FrstNm ";
        first = false;
    }
    query += ";";
    // ...
    

    Second solution, you are using wildcard. If your parameter is empty, just put a ‘%’ in your query, which will match all corresponding strings:

    query.setParameter("FrstNm ", (firstName != null ? firstName : "") + '%');
    query.setParameter("LastNm ", (lastName != null ? lastName : "") + '%');
    

    If firstName is empty, your query will look like:

    ... cst.cstFrstNm like '%' ...
    

    and will match all the values. It will result in the parameter not filtering any result, with a behaviour similar as if it wasn’t present.

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

Sidebar

Related Questions

I'm writing a J2SE app (no enterprise container) that uses JPA for persistence. Here
I'm writing an application that uses JPA for persistence. Currently I'm testing with Hibernate
I am writing an application that will move data from one database to another
I'm writing a JPQL query that joins across three tables. In my resultlist I
I'm writing a Seam-based application, making use of JPA/Hibernate and Hibernate Search (Lucene). I
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
Writing documentation in html requires some code examples. What to do with characters that
Writing a python program, and I came up with this error while using the
I'm just writing a small java application and I would like to be able

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.