简体   繁体   English

带有两个参数的休眠IN查询

[英]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. 另外,我想将值作为查询参数插入以防止sql注入。

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. 我认为没有针对此的干净的Hibernate解决方案。 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. 即使采用这种方法,您最终仍会构建类似于最终查询的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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