我有以下HQL查询
StringBuilder selectBuilder = new StringBuilder(
"select new support.dto.SrsRequirementStatus( s.id, sm.id, sm.name, "
+ "sr.id, sr.requirement, sr.reference, "
+ "sca.code_id, sca.code_name, spa.code_id, spa.code_name, ssa.code_id, ssa.code_name, "
+ "function('cr_min_query', s.id, 'SRS') as minuser, "
+ "function('cr_max_query', s.id, 'SRS') as maxuser ) "
+ "from SRS s "
+ "inner join s.domain d "
+ "inner join s.project p "
+ "inner join s.srsModule sm "
+ "inner join s.srsRequirements sr "
+ "inner join sr.category sca "
+ "inner join sr.priority spa "
+ "inner join sr.status ssa ");
StringBuilder whereCauseBuilder = new StringBuilder("where d.code_id = :domainId and p.project_id = :projectId ");
使用以下构造函数
public SrsRequirementStatus( Integer srsId, Integer srsModuleId, String srsModuleName,
Integer requirementId, String requirement, String reference, String categoryId, String categoryName,
String priorityId, String priorityName, String statusId, String statusName,
String createdBy, String updatedBy ) {
this.srsId = srsId;
this.srsModuleId = srsModuleId;
this.srsModuleName = srsModuleName;
this.requirementId = requirementId;
this.requirement = requirement;
this.categoryId = Integer.valueOf( categoryId );
this.priorityId = Integer.valueOf( priorityId );
this.statusId = Integer.valueOf( statusId );
this.categoryName = categoryName;
this.priorityName = priorityName;
this.statusName = statusName;
this.reference = reference;
this.createdBy = createdBy;
this.updatedBy = updatedBy;
}
自定义SQLDialect类
public class CustomMySQLDBDialect extends MySQL5InnoDBDialect {
public CustomMySQLDBDialect() {
super();
registerFunction( "cr_max_query", new StandardSQLFunction( "cr_max_query" ) );
registerFunction( "cr_min_query", new StandardSQLFunction( "cr_min_query" ) );
}
}
上面提到的HQL查询生成以下例外
我的 SQL 查询类似于上面的 HQL 查询,只返回两行,没有任何 NULL 列值。
java.lang.NullPointerException
at org.hibernate.internal.util.ReflectHelper.getConstructor(ReflectHelper.java:309)
at org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:174)
at org.hibernate.hql.internal.ast.tree.ConstructorNode.prepare(ConstructorNode.java:144)
at org.hibernate.hql.internal.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:1091)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2328)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2194)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1476)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:573)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
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:298)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1821)
at support.DAO.ImplDeveloperDAO.findSrsRequirements(ImplDeveloperDAO.java:2046)
at support.service.ImplDeveloperService.findSrsRequirements(ImplDeveloperService.java:986)
at support.service.ImplDeveloperService$$FastClassBySpringCGLIB$$90525943.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at support.service.ImplDeveloperService$$EnhancerBySpringCGLIB$$c67eb653.findSrsRequirements(<generated>)
at support.controller.DeveloperController.findSrsDetails(DeveloperController.java:1694)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:722)
答案 0 :(得分:0)
在Custom MySQLDialect类
中进行一些更改后,它工作正常我改变了
registerFunction("cr_max_query", new StandardSQLFunction("cr_max_query"));
到
registerFunction("cr_max_query", new StandardSQLFunction("cr_max_query", StringType.INSTANCE));
还从HQL查询中删除了 function 关键字,并简单地调用了这样的函数
[...]
+ "cr_min_query(s.id, 'SRS') as minuser, "
[...]
我正在关注使用功能关键字的https://www.thoughts-on-java.org/database-functions/,并且在我的情况下它不起作用,不知道为什么?