maxtorzito
maxtorzito

Reputation: 318

How to convert SQL Query using CriteriaQuery in JPA

I want to convert my sql query from SQL to Criterias (i dont want to use JPQL), i have this sql query:

SELECT * FROM (
SELECT CONCAT_WS(' ',p.first_name, p.middle_name, p.last_name) AS fullname FROM persons p) AS tmp
WHERE fullname LIKE '%cetina avila%' ORDER BY fullname;

How is the best way to do this?, Im trying to search full names from my talbe persons.

Thanks

Upvotes: 0

Views: 976

Answers (1)

carbontax
carbontax

Reputation: 2184

One way to do this is to use an @Formula mapping on your Person class like this

@Formula("CONCAT(first_name, ' ', middle_name, ' ', last_name)")
private String fullname;

Then in your Criteria you can search it like any normal field.

See Calculated property with JPA / Hibernate for more on calculated fields.

Upvotes: 1

Related Questions