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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:45:07+00:00 2026-06-19T04:45:07+00:00

Suppose you have the following data in table corresponding to the class Person ,

  • 0

Suppose you have the following data in table corresponding to the class Person, what is the correct way to search null-safely for the concatenation of fields name1 and name2?

@Entity
public class Person {
    Long id;
    String name1;
    String name2;
    // Getters and setters omitted for brevity
}
id |  name1  | name2   
------------------------
1  |  Foo    | null
2  |  null   | Bar
3  |  Foo    | Bar

By default the concatentation of two columns results in null if either is null.

public List<String> nameConcatenations() {
    JPAQuery q = new JPAQuery(entityManager);
    QPerson person = QPerson.person;
    StringExpression nameConcatenation = person.name1.concat(person.name2);
    return q.from(person).list(nameConcatenation)
}

The above code results in the following list:

null
null
FooBar
  • 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-19T04:45:08+00:00Added an answer on June 19, 2026 at 4:45 am

    One easy way is to use the SQL’s COALESCE function which has an equivalent in Querydsl

    public List<String> nameConcatenations() {
        JPAQuery q = new JPAQuery(entityManager);
        QPerson person = QPerson.person;
        StringExpression nameConcatenation = emptyIfNull(person.name1)
            .concat(emptyIfNull(person.name2));
        return q.from(person).list(nameConcatenation)
    }
    
    private static StringExpression emptyIfNull(StringExpression expression) {
        return expression.coalesce("").asString();
    }
    

    Will result in the following list:

    Foo
    Bar
    FooBar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following table ( JS Fiddle ): <table class=data-table> <thead> <tr>
Suppose we have the following table data: ID parent stage submitted 1 1 1
Suppose I have the following types of data: class Customer { String id; //
Consider the following data model: Suppose I have a table called SuperAwesomeData where each
Suppose I have a table with following data: gameId difficultyLevel numberOfQuestions -------------------------------------------- 1 1
Suppose I have the following entities: @Entity @Table(name = A) public class A implements
Suppose I have the following data frame: Data1 X1 X2 1 15 1 2
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
I have a table in database which has following structure(data) : +--------------------------+ Organization +--------------------------+
Suppose I have the following html: var testString = ' <span id='id'> <div> <table>

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.