[英]SpringDataJpa: Multiple ContainingIgnoreCase
I have something like below. 我有类似下面的东西。 basically I want to get all the books contains certain keywords in their title or description. 基本上我想让所有的书都在其标题或描述中包含某些关键词。 Spring keep giving me error on this method. Spring继续给我这个方法的错误。 Single select of either Title or Descr works, but I am just not able to put them together. 可以单独选择Title或Descr,但我无法将它们组合在一起。 What am I missing? 我错过了什么?
works: 作品:
findBookByTitleContainingIgnoreCase(String keyWord)
findBookByDescrContainingIgnoreCase(String keyWord)
doesn't work: findBookByTitleContainingIgnoreCaseOrDescrContainingIgnoreCase(String keyWord)
不起作用: findBookByTitleContainingIgnoreCaseOrDescrContainingIgnoreCase(String keyWord)
This will work: 这将有效:
findByTitleContainingIgnoreCaseOrDescrContainingIgnoreCase(String title, String descr);
You can then call this method as bookRepository.findByTitleContainingIgnoreCaseOrDescrContainingIgnoreCase(keyword, keyword)
然后,您可以将此方法称为bookRepository.findByTitleContainingIgnoreCaseOrDescrContainingIgnoreCase(keyword, keyword)
An easy way to remember how to form queries with Spring Data JPA is to keep in mind that method names eventually become JDBC PreparedStatement
s. 记住如何使用Spring Data JPA形成查询的简单方法是记住方法名称最终成为JDBC PreparedStatement
。 Each keyword (class attribute) in the method name becomes a parameter in the PreparedStatement
. 方法名称中的每个关键字(类属性)都成为PreparedStatement
的参数。 Consequently, each of the parameters must be bound to a value when the PreparedStatement
is executed. 因此,在执行PreparedStatement
时,每个参数都必须绑定到一个值。 Therefore, the method must have equal number of arguments for the PreparedStatement
to execute correctly. 因此,该方法必须具有相同数量的参数才能使PreparedStatement
正确执行。
When you apply this thinking to your problem, you will see that your method name has two keywords - Title
and Descr
but only one parameter. 当您将此思维应用于您的问题时,您将看到您的方法名称有两个关键字 - Title
和Descr
但只有一个参数。 So, the second parameter in the PreparedStatement
remains unbound when the PreparedStatement
is executed; 因此,在第二个参数PreparedStatement
的时保持未结合PreparedStatement
执行; hence the error. 因此错误。 Supplying both parameters solves the problem. 提供这两个参数可以解决问题。
Sample code with working test cases on Github . 在Github上使用工作测试用例的示例代码。
findBookByTitleContainingIgnoreCaseOrDescrContainingIgnoreCase(String keyWord, String keyWord2);
fixed my problem. 解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.