简体   繁体   English

Trim() Function 与 Spring JPA 类似查询方法

[英]Trim() Function with Spring JPA like Query Methods

I am making a rest service that internally is using JPA to find data with a prefix (for instance: Information prefix).我正在制作一个 rest 服务,该服务在内部使用 JPA 来查找带有前缀的数据(例如:信息前缀)。

My query method with Spring data looks like this:我使用 Spring 数据的查询方法如下所示:

List<Entity> findAllByFieldStartsWithIgnoreCase(String field);

Everything is working fine, however, there are data in the database that have the Information prefix, but also have a white space, like in the followings example:一切正常,但是,数据库中的数据具有 Information 前缀,但也有空格,如下例所示:

Field:场地:

Information 1
    Information 2
 Information 3
Information 4 

Like you can see in the above examples, Information 1 does not have some white space, but Information 2 and 3 have one, or more, white spaces.就像您在上面的示例中看到的那样,信息 1 没有一些空白,但信息 2 和 3 有一个或多个空白。

I am looking the way to ignore these spaces, I know that sql has the trim function to make it, but in JPA repository with Spring , with query methods, does not exist: I am looking the way to ignore these spaces, I know that sql has the trim function to make it, but in JPA repository with Spring , with query methods, does not exist:

I would like to find a function to type the query methods like this:我想找一个 function 来键入这样的查询方法:

List<Entity> findAllByField  Trim   StartsWithIgnoreCase(String field);

In order to execute trim function before the StartWith.为了在 StartWith 之前执行修剪 function。

I would apprecite your help, regards.我会感谢你的帮助,问候。

You can use a JPQL query here using the @Query annotation.您可以使用@Query注释在此处使用 JPQL 查询。 For example, the following snippet should give you a prefix search on a trimmed column例如,下面的代码片段应该给你一个修剪列的前缀搜索

@Query("select e from Entity e where trim(e.field) like concat(:field,'%')")
List<Entity> findAllByFieldStartsWithIgnoreCase(@Param("field") String field);

Here's a resource on how to use native/JPQL queries with Hibernate这是有关如何将本机/JPQL 查询与 Hibernate 一起使用的资源

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

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