简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM