使用类似 %?1% 的 @Query 注释 [英] @Query annotation using like %?1%

查看:14
本文介绍了使用类似 %?1% 的 @Query 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个这样的查询

@Query("select p from Product p where p.name = ?1 or p.desc like %?1%")

但它给了我例外

org.hibernate.hql.ast.QuerySyntaxException: unexpected token: % near line 1,

我尝试将 % 替换为 '%' 或像这样连接查询字符串:"select ... like '%'" + "?1" + "'%'" 但没有成功,请帮助我

I tried replacing % with '%' or concatenating the query string like this: "select ... like '%'" + "?1" + "'%'" but with no luck, please help me

推荐答案

如果您使用的是 Spring Data JPA 1.3.1 或更高版本,您需要执行以下操作:

If you are using Spring Data JPA version 1.3.1 or later you need to do the following:

@Query("select p from Product p where p.name = :name or p.desc like %:name%")
public List<Product> searchByName(@Param("name") String name);

查看 这篇博文了解更多详情

Check out this blog post for more details

在 Spring Data JPA 1.3.1 之前,您不能在 @Query 注释中使用 %,而是需要将其添加到参数本身中

Prior to Spring Data JPA 1.3.1, you could not use % in the @Query annotation, but instead you needed to add it in the argument itself

这篇关于使用类似 %?1% 的 @Query 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆