0

I have entity where I have fisrtName and lastname but in search I need to implement search for fullName. How can I combine this two fields in search in JPA

right now I have something like this:

Query query = em.createQuery("SELECT u from Client u where u.firstName like :firstName   or u.lastName like :lastName or u.documentID like :documentID");

I guess it is very commonly used feature but I don't know how to do that. Please help

Irakli
  • 973
  • 3
  • 19
  • 45
  • 2
    what is "full name" ? CONCAT(firstName, lastName, " ") ? JPQL has CONCAT. What have you tried ? – Neil Stockton Feb 23 '16 at 16:13
  • Thanks that's exactly what I needed. `Query query = em.createQuery("SELECT u from Client u where CONCAT(u.firstName, ' ',u.lastName ) like :firstName or CONCAT(u.lastName, ' ',u.firstName ) like :lastName or u.documentID like :documentID");` write down your answer I will mark it as ANSWER – Irakli Feb 23 '16 at 16:30

1 Answers1

2

You haven't defined what is "full name". If you mean a concatenation of the two "name" fields, then you can use a construct in JPQL such as

CONCAT(u.firstName, ' ', u.lastName)

to put them together with a space in between, and that ought to give enough idea how to get the precise query needed.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29