[英]Passing array or list of strings in IN clause of a sql query
I am new to Java, so please forgive me if it is a naive question.我是 Java 的新手,所以如果这是一个幼稚的问题,请原谅我。 I have a string that is in comma separated like this
"str1, str2,str3"
.I need to query data from database that matches that list of strings.我有一个用逗号分隔的字符串,像这样
"str1, str2,str3"
。我需要从数据库中查询与该字符串列表匹配的数据。 I know I can convert "str1,str2,str3"
to an array.我知道我可以将
"str1,str2,str3"
转换为数组。 But would also need to pass this in a IN clause of sql query like this但也需要在 sql 查询的 IN 子句中传递这个
select * from Customer where name IN ('str1','str2','str3')
How can I accomplish that since after the conversion of "str1,str2,str3"
to an array won't be legal to pass it in IN clause?我怎么能做到这一点,因为在将
"str1,str2,str3"
转换为数组之后,在 IN 子句中传递它是不合法的? Any utility method in hibernate that can help taking care of this issue? hibernate 中的任何实用方法可以帮助解决此问题?
Thank you in advance先感谢您
You can try to use something like this:你可以尝试使用这样的东西:
List<Customer> customers = entityManager.createQuery(
"select c " +
"from Customer c " +
"where c.name in :names", Customer.class )
.setParameter( "names", Arrays.asList( "str1", "str2" ) )
.getResultList();
See also this section of hibernate documentation.另请参阅 hibernate 文档的此部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.