[英]How to use SQL functions UPPER() and LOWER() with JPA query methods
I have a JPA Query method that gets rows by a column value like this: 我有一个JPA Query方法,该方法通过像这样的列值获取行:
findAllByLastNameOrderBySortKeyAsc(String name)
But I would like the query to be case insensitive. 但是我希望查询不区分大小写。 In SQL I can use the
upper()
function like this 在SQL中,我可以像这样使用
upper()
函数
select * from table_name where upper(last_name) like '%NAME%' order by sort_key asc
but I haven't been able to find any examples or documentation on how I can implement this with named queries. 但是我找不到任何有关如何使用命名查询实现此功能的示例或文档。
Update: 更新:
Although it does not answer the question of how to use upper()
or lower()
with named queries, I was able to solve the problem of a case insensitive query by using IgnoreCase
in the named query as follows: 尽管它不能回答如何在命名查询中使用
upper()
或lower()
问题,但我能够通过在命名查询中使用IgnoreCase
来解决不区分大小写的查询的问题,如下所示:
findAllByLastNameIgnoreCaseOrderBySortKeyAsc(String name)
As explained in the spring-data-jpa documention , you must use the IgnoreCase keyword to be case insensitive. 如spring-data-jpa 文档中所述 ,您必须使用IgnoreCase关键字不区分大小写。
Your method definition should be like this: 您的方法定义应如下所示:
findAllByLastNameIgnoreCaseOrderBySortKeyAsc(String name)
The resulting SQL will be: … where UPPER(x.firstame) = UPPER(?1) 生成的SQL将是: …其中UPPER(x.firstame)= UPPER(?1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.