我正在尝试使用 JPA 在 SpringBoot 应用程序中编写自定义查询,我有一个带有值列表的 IN 子句(列表也可以为 null)。我写了一个这样的查询:
@Query( "SELECT new com.mypackage.model.CustomOutput( AVG(t.time) ) " +
"FROM MyTable t WHERE " +
"t.time IS NOT NULL AND t.updatedTime > :after AND t.updatedTime < :before " +
"AND " +
"( (:filterA) IS NULL OR t.columnA IN (:filterA) ) AND " +
"( (:filterB) IS NULL OR t.columnB IN (:filterB) ) AND " +
"( (:filterC) IS NULL OR t.columnC IN (:filterC) ) AND " +
"( (:filterD) IS NULL OR t.columnD IN (:filterD) )"
)
CustomOutput findCustomOutput(
@Param("filterA") List<String> filterA,
@Param("filterC") List<String> filterC,
@Param("filterB") List<String> filterB,
@Param("filterD") List<String> filterD,
@Param("after") Date after,
@Param("before") Date before
);
所有过滤器 - A、B、C、D 可以是值列表或 null,当任何过滤器为
时查询工作正常当值超过一个时查询失败 (例如:过滤器 - A、B、C 为空,D 是两个值的列表)。这 例外是:
2024-06-28 12:32:32,543 [collectorsCluster_Worker-1] WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 920, SQLState: 42000
2024-06-28 12:32:32,544 [collectorsCluster_Worker-1] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ORA-00920: invalid relational operator
2024-06-28 12:32:32,559 [collectorsCluster_Worker-1] ERROR c.c.d.job.servicenow.ServiceNowJob - Unexpected error occurred -
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:259)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:145)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
at com.sun.proxy.$Proxy231.findCustomOutput(Unknown Source)
数据库是Oracle,有人可以指出语法中的问题吗?
问题出在
(:filterA) IS NULL
子句中,该子句对于大于一个元素的集合会失败。
请参阅检查 Spring data JPA 查询中的 List 参数是否为空以获取建议的解决方案
根据@Lesiak 的回答修改我的自定义查询:
@Query( "SELECT new com.mypackage.model.CustomOutput( AVG(t.time) ) " +
"FROM MyTable t WHERE " +
"t.time IS NOT NULL AND t.updatedTime > :after AND t.updatedTime < :before " +
"AND " +
"( COALESCE(:filterA, null) IS NULL OR t.columnA IN (:filterA) ) AND " +
"( COALESCE(:filterB, null) IS NULL OR t.columnB IN (:filterB) ) AND " +
"( COALESCE(:filterC, null) IS NULL OR t.columnC IN (:filterC) ) AND " +
"( COALESCE(:filterD, null) IS NULL OR t.columnD IN (:filterD) )"
)
CustomOutput findCustomOutput(
@Param("filterA") List<String> filterA,
@Param("filterC") List<String> filterC,
@Param("filterB") List<String> filterB,
@Param("filterD") List<String> filterD,
@Param("after") Date after,
@Param("before") Date before
);