简体   繁体   中英

Hibernate IN query with two parameters

I need to construct a hibernate query that would do something like this:

select p from Person p where (p.name, p.surname) in (('Peter', 'Black'), ('Thomas', 'Peterson'), ('Julia', 'Cook'))

The length of the name/surname list is not defined upfront and the name and surname can contain any characters (the name and surname naming is used just for the illustration purposes)

Also I would like to insert the values as a query parameter to prevent sql injection.

solutions that is not acceptable:

(p.name || '--' || p.surname) in (:mergedNameSurname)

solution that I would like to avoid:

((p.name = :name1 and p.surname = :surname1) or (p.name = :name2 and p.surname = :surname2) ...)

Do you have some idea how this could be done?

I do not think there is a clean Hibernate solution for this. You either take one of the two approaches you suggested or add a denormalized column which represents the concatenation of the two columns. Even with that approach you will end up building something similar to your final query though.

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