我有一个Spring JPA存储库,我有一个自定义查询:
@Query("select aif from AssetInFolder aif "
+ "where (aif.asset.name like %:name% "
+ "or aif.folder.name like %:name% "
+ "or aif.asset.assetCode like :code%) "
+ "and aif.folder.assetVersion.id = :version")
public List<AssetInFolder> searchForAsset(@Param("name") String name, @Param("code") String code, @Param("version") int version);
我想确保查询的第一部分(在'和'之前)是敏感的。我已经尝试将其更改为:
@Query("select aif from AssetInFolder aif "
+ "where (UPPER(aif.asset.name) like UPPER(%:name%) "
+ "or UPPER(aif.folder.name) like UPPER(%:name%) "
+ "or UPPER(aif.asset.assetCode) like UPPER(:code%)) "
+ "and aif.folder.assetVersion.id = :version")
但是当Spring尝试初始化应用程序上下文时,我得到一个NullPointer:
Caused by: java.lang.NullPointerException: null
at org.hibernate.hql.internal.antlr.HqlBaseParser.identPrimary(HqlBaseParser.java:4285) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:980) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3609) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:3387) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.multiplyExpression(HqlBaseParser.java:3259) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.additiveExpression(HqlBaseParser.java:2964) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.concatenation(HqlBaseParser.java:597) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.relationalExpression(HqlBaseParser.java:2898) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.equalityExpression(HqlBaseParser.java:2591) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2555) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:2471) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:2436) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.expression(HqlBaseParser.java:2146) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.expressionOrVector(HqlBaseParser.java:4632) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:942) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3609) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:3387) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.multiplyExpression(HqlBaseParser.java:3259) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.additiveExpression(HqlBaseParser.java:2964) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.concatenation(HqlBaseParser.java:597) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.relationalExpression(HqlBaseParser.java:2730) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.equalityExpression(HqlBaseParser.java:2591) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2555) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:2471) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:2436) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.expression(HqlBaseParser.java:2146) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalExpression(HqlBaseParser.java:1922) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.whereClause(HqlBaseParser.java:481) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:737) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:323) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:186) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:279) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:298) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1825) ~[hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291) ~[hibernate-entitymanager-5.0.2.Final.jar:5.0.2.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_05]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_05]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_05]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_05]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344) ~[spring-orm-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at com.sun.proxy.$Proxy48.createQuery(Unknown Source) ~[na:na]
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86) ~[spring-data-jpa-1.9.0.RELEASE.jar:na]
... 80 common frames omitted
然而,更奇怪的是,如果我写下删除一些UPPER
来电,我就可以开始了:
@Query("select aif from AssetInFolder aif "
+ "where (UPPER(aif.asset.name) like UPPER(%:name%) "
+ "or aif.folder.name like %:name% "
+ "or aif.asset.assetCode like :code%) "
+ "and aif.folder.assetVersion.id = :version")
有人有任何想法吗?我正在使用Spring 4.2.2和Spring Data JPA 1.9.0
答案 0 :(得分:0)
我最终只在列名称上调用UPPER并依赖调用者以大写形式传递参数。所以我的查询结果是:
@Query("select aif from AssetInFolder aif "
+ "where (UPPER(aif.asset.name) like %:name% "
+ "or UPPER(aif.folder.name) like %:name% "
+ "or UPPER(aif.asset.assetCode) like :code%) "
+ "and aif.folder.assetVersion.id = :version")
这解决了这个问题,但是我仍然不确定原始查询为什么不起作用。