从1.5升级后,Spring Boot 2 @Query命名参数绑定值解析陷入混乱

时间:2019-04-03 12:36:28

标签: spring spring-boot spring-data-jpa spring-data spring2.x

我们使用SpringBoot 1.5进行以下工作查询:

@Query(value = "SELECT DISTINCT c FROM Customer c INNER JOIN c.industry i WHERE " +
            "c.role IN :roleFilter " +
            "AND (:#{#industryFilter.size()} = 1 OR i.id IN :industryFilter) " +
            "AND (:searchString IS NULL " +
            "OR CONCAT_WS(' ', c.name, c.name2) LIKE CONCAT('%', :searchString, '%') " +
            "OR CONCAT_WS(' ', c.name2, c.name) LIKE CONCAT('%', :searchString, '%')) " +
            "AND (:includeDeleted = true OR c.deletedDate is NULL)",
            countQuery = "SELECT COUNT(DISTINCT c) FROM Customer c INNER JOIN c.industry i WHERE " +
                    "c.role IN :roleFilter AND " +
                    "(:#{#industryFilter.size()} = 1 OR i.id IN :industryFilter) " +
                    "AND (:searchString IS NULL " +
                    "OR CONCAT_WS(' ', c.name, c.name2) LIKE CONCAT('%', :searchString, '%') " +
                    "OR CONCAT_WS(' ', c.name2, c.name) LIKE CONCAT('%', :searchString, '%')) " +
                    "AND (:includeDeleted = true OR c.deletedDate is NULL)")
    Page<Customer> findCustomers(@Param("roleFilter") Set<Role> roleFilter,
                                 @Param("industryFilter") Set<String> industryFilter,
                                 @Param("searchString") String searchString,
                                 @Param("includeDeleted") boolean includeDeleted, Pageable pageable);

请注意我们如何将输入传递给LIKE:CONCAT('%', :searchString, '%')

springBootVersion = '1.5.17.RELEASE'升级到springBootVersion = '2.1.3.RELEASE'(我们使用Gradle)后,该查询将在运行时失败,并发生以下异常:

org.hibernate.QueryException:未绑定的命名参数:includeDeleted

CONCAT('%', :searchString, '%')代替%:searchString%可解决此问题。

我的问题是:为什么?

通过进入调试模式并遵循完整的调用栈,我可以看到正确地从方法调用中正确检索了参数,如在205行的JdkDynamicAopProxy中进行的调用Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);导致:< / p>

argsToUse = {Object[5]@15562} 
 0 = {HashSet@15491}  size = 4
 1 = {HashSet@15628}  size = 1
 2 = null
 3 = {Boolean@15629} false
 4 = {PageRequest@15490} "Page request [number: 0, size 20, sort: name: ASC,name2: ASC]"

到目前为止,一切都很好。然后,我们继续操作,调用方法也已正确解析:

parameterTypes = {Class[5]@15802} 
 0 = {Class@198} "interface java.util.Set"
 1 = {Class@198} "interface java.util.Set"
 2 = {Class@311} "class java.lang.String"
 3 = {Class@15811} "boolean"
 4 = {Class@9875} "interface org.springframework.data.domain.Pageable"

然后,我们走得更远,然后转到RepositoryFactorySupport第599行,调用private Object doInvoke(MethodInvocation invocation) throws Throwable,它使用来自内部类private final Map<Method, RepositoryQuery> queries;的{​​{1}}(我不确定何时/如何此变量已创建并填充),其中包含我的存储库界面中所有用public class QueryExecutorMethodInterceptor implements MethodInterceptor注释的查询。

对于我们的特定情况,它包含一个与我正在调用的查询( findCustomers )匹配的条目(最后一个):

@Query

展开该条目,我可以看到错误的出处,queries = {HashMap@16041} size = 3 0 = {HashMap$Node@16052} "public abstract com.swisscom.psp.domain.Customer com.swisscom.psp.repository.CustomerRepository.getOne(java.lang.String)" -> 1 = {HashMap$Node@16055} "public abstract boolean com.swisscom.psp.repository.CustomerRepository.existsWithRole(java.lang.String,java.util.Set)" -> 2 = {HashMap$Node@16058} "public abstract org.springframework.data.domain.Page com.swisscom.psp.repository.CustomerRepository.findCustomers(java.util.Set,java.util.Set,java.lang.String,boolean,org.springframework.data.domain.Pageable)" -> 命名参数的绑定根本不存在:

:includeDeleted

现在,我已经有了前面提到的修复程序,但是我仍然非常想知道以下内容,以供将来参考:

  1. 何时以及如何创建和填充value = {SimpleJpaQuery@16060} query = {ExpressionBasedStringQuery@16069} query = "SELECT DISTINCT c FROM Customer c INNER JOIN c.industry i WHERE c.role IN :roleFilter AND (:__$synthetic$__1 = 1 OR i.id IN :industryFilter) AND (:searchString IS NULL OR CONCAT_WS(' ', c.name, c.name2) LIKE CONCAT('%', :searchString, '%') OR CONCAT_WS(' ', c.name2, c.name) LIKE CONCAT('%', :searchString, '%')) AND (:includeDeleted = true OR c.deletedDate is NULL)" bindings = {ArrayList@16089} size = 6 0 = {StringQuery$InParameterBinding@16092} "ParameterBinding [name: roleFilter, position: null, expression: null]" 1 = {StringQuery$ParameterBinding@16093} "ParameterBinding [name: __$synthetic$__1, position: null, expression: #industryFilter.size()]" 2 = {StringQuery$InParameterBinding@16094} "ParameterBinding [name: industryFilter, position: null, expression: null]" 3 = {StringQuery$ParameterBinding@16095} "ParameterBinding [name: searchString, position: null, expression: null]" 4 = {StringQuery$ParameterBinding@16096} "ParameterBinding [name: searchString, position: null, expression: null]" 5 = {StringQuery$ParameterBinding@16097} "ParameterBinding [name: searchString, position: null, expression: null]" 变量?
  2. 究竟是什么导致此错误?我在升级过程中错过了什么吗?我是否正在使用/混合已弃用的逻辑/错误的逻辑,并且应该进一步更改代码?

我们的数据库是MariaDB 10.1.36

编辑:在所有发生此行为的地方(在某些地方仍然发生),unbound参数始终是最后一个

EDIT2:升级后其他人也有类似的行为,为什么会发生这种情况? reference

EDIT3:reference,并且也报告了这种奇怪的行为。足够有趣的是,如果我将已经连接的输入传递给:searchString(例如:%SOMETHING%),并且如果我留下%:searchString%,我确实会得到异常,但是我没有得到异常。是的,最后移动这些参数可以解决我在绑定时遇到的一些错误。

EDIT4:也许与bug相关?

显然发生了一件奇怪的事情,所以:此绑定解析是如何发生的

预先感谢,祝您愉快

1 个答案:

答案 0 :(得分:1)

据我所知,实际上,您的两种方法都不是在此处使用通配符占位符处理LIKE的正确方法。相反,LIKE表达式应为:

LIKE :searchString

您应该绑定到此参数:searchString

String searchString = "bananas";
String param = "%" + searchString + "%";
// then bind param to :searchString

也就是说,将整个字符串和%通配符绑定在一起。然后,让数据库担心如何对其进行转义。