Spring 数据 JPA:HQL 中带有 IN 子句的 LOWER() 或 UPPER()

Spring Data JPA: LOWER() or UPPER() with IN clause in HQL

我在这里尝试根据字符串集合对“代码”列进行不区分大小写的搜索。 但它在 HQL 验证期间失败,在 SQLFunctionRegistry.java.

我正在使用 Oracle 数据库。

@Query(value = "SELECT A FROM A WHERE bro = :bro AND LOWER(code) IN :codes")
List<A> findByBroAndCodeIn(@Param("bro") boolean bro, @Param("codes") Collection<String> codes);
    Caused by: java.lang.NullPointerException
        at java.lang.String$CaseInsensitiveComparator.compare(String.java:1192)
        at java.lang.String$CaseInsensitiveComparator.compare(String.java:1186)
        at java.util.TreeMap.getEntryUsingComparator(TreeMap.java:376)
        at java.util.TreeMap.getEntry(TreeMap.java:345)
        at java.util.TreeMap.get(TreeMap.java:278)
        at org.hibernate.dialect.function.SQLFunctionRegistry.findSQLFunction(SQLFunctionRegistry.java:45)
        at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.findSQLFunction(SessionFactoryHelper.java:369)
        at org.hibernate.hql.internal.ast.tree.IdentNode.getDataType(IdentNode.java:325)
        at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:156)
        at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:920)
        at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:688)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:673)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:309)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:257)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
        at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
        at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302)
        at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240)
        at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1894)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347)
        at com.sun.proxy.$Proxy123.createQuery(Unknown Source)
        at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86)

我无法在单独的 OR 条件下使用 LIKE 函数,因为我不知道上面集合中的字符串数量。

提前感谢您的帮助。

根据@Eklavya 的建议

Identification Variables are necessary for JPQL to access entity field and I'm getting an NPE because code is null as not mapped properly

提供别名已解决问题。

@Query(value = "SELECT a FROM A a WHERE a.bro = :bro AND LOWER(a.code) IN :codes")
List<A> findByBroAndCodeIn(@Param("bro") boolean bro, @Param("codes") Collection<String> codes);