简体   繁体   English

SpringDataJpa:Multiple ContainingIgnoreCase

[英]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. 当您将此思维应用于您的问题时,您将看到您的方法名称有两个关键字 - TitleDescr但只有一个参数。 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.

相关问题 SpringDataJPA @ManyToMany crud - SpringDataJPA @ManyToMany crud 如何让@Query 在 JPA 中执行 ContainingIgnoreCase 函数? - How to make @Query perform ContainingIgnoreCase function in JPA? "EntityManager 为 null 甚至添加了 springDataJpa 依赖后" - EntityManager is null evenAfter added the springDataJpa dependency 是否可以在 SpringDataJpa @QueryHint 注释中指定 fetchgraph/loadgraph - Is it possible to specify fetchgraph/loadgraph in SpringDataJpa @QueryHint annotation SpringDataJPA持续存在:仅在查询时填充字段,但不保存 - SpringDataJPA persist: only populate a field when querying, but not save SpringDataJPA只为多对多关系保存映射表 - SpringDataJPA only save mapping table for Many to Many relationship SpringDataJPA保存OneToOne关系获取无法添加或更新子行:外键约束失败 - SpringDataJPA save OneToOne relationship getting Cannot add or update a child row: a foreign key constraint fails 由于双向@ManyToMany 关系 SpringDataJPA 上的堆栈溢出错误,我无法创建实体或获取实体列表 - I am unable to create entity or fetch list of entities due to stack overflow error on bi-directional @ManyToMany relationship SpringDataJPA 带有多个动作侦听器的多个按钮 - Multiple button with multiple actionlistener 多次抛出多次测试 - Multiple tests with multiple throws
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM