您好我的DAO类中有如下所示的HQL查询。
String queryString1 = "select new com.apple.ist.cm.ruleadmin.model.SearchQuestionnaire(qc.questionCategoryID,qc.questionCategoryName, qn.questionReferenceString, qn.question,qao.answerOptionGroupID,answerOptionGroup,answerStringValue) " +" from Questionnaire qn,QuestionCategory qc,QuestionAnswerOption qao,AnswerOptionGroup aog,AnswerString ans "+"where qn.questionCategoryID = qc.questionCategoryID and qn.questionID=qao.questionID and qao.answerOptionGroupID=aog.answerOptionGroupID and qao.answerStringID=ans.answerStringID";
SearchQuestionnaire quest = null;
query1 = (List<SearchQuestionnaire>) hibernateTemplate.find(queryString1);
我将所有模型映射到5个表:
SearchQuestionnaire.java是一个虚拟的pojo,用于传递查询,其中包含上述查询中所有选定列的getter setter。
public class SearchQuestionnaire {
private long questionCategoryID;
private String questionCategoryName;
private String questionReferenceString;
private long questionCategory;
private String question;
private long answerOptionGroupID;
private String answerOptionGroup;
private String answerStringValue;
public SearchQuestionnaire(){
}
public SearchQuestionnaire(long answerOptionGroupID,String answerOptionGroup){
this.questionCategoryID = questionCategoryID;
this.questionCategoryName = questionCategoryName;
this.questionReferenceString = questionReferenceString;
this.question = question;
this.answerOptionGroupID = answerOptionGroupID;
this.answerOptionGroup = answerOptionGroup;
this.answerStringValue = answerStringValue;
}
........所有的吸气剂和二传手 }
以上是上面的错误:
20:05:50,561 ERROR [STDERR] java.lang.NullPointerException
20:05:50,562 ERROR [STDERR] at org.hibernate.util.ReflectHelper.getConstructor(ReflectHelper.java:195)
20:05:50,562 ERROR [STDERR] at org.hibernate.hql.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:137)
20:05:50,563 ERROR [STDERR] at org.hibernate.hql.ast.tree.ConstructorNode.prepare(ConstructorNode.java:111)
20:05:50,563 ERROR [STDERR] at org.hibernate.hql.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:862)
20:05:50,563 ERROR [STDERR] at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:1956)
20:05:50,563 ERROR [STDERR] at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1825)
20:05:50,563 ERROR [STDERR] at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1394)
20:05:50,563 ERROR [STDERR] at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:553)
20:05:50,563 ERROR [STDERR] at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
20:05:50,563 ERROR [STDERR] at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
20:05:50,563 ERROR [STDERR] at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251)
20:05:50,563 ERROR [STDERR] at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
20:05:50,563 ERROR [STDERR] at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
20:05:50,563 ERROR [STDERR] at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
20:05:50,564 ERROR [STDERR] at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
20:05:50,564 ERROR [STDERR] at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
20:05:50,564 ERROR [STDERR] at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
20:05:50,564 ERROR [STDERR] at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
20:05:50,564 ERROR [STDERR] at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)
20:05:50,564 ERROR [STDERR] at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:914)
20:05:50,564 ERROR [STDERR] at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:1)
20:05:50,564 ERROR [STDERR] at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
20:05:50,564 ERROR [STDERR] at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
20:05:50,564 ERROR [STDERR] at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912)
20:05:50,564 ERROR [STDERR] at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:904)
20:05:50,565 ERROR [STDERR] at com.apple.ist.cm.ruleadmin.dao.QuestionnaireDAOImpl.listQuestionnaire(QuestionnaireDAOImpl.java:300)
20:05:50,565 ERROR [STDERR] at com.apple.ist.cm.ruleadmin.controllers.QuestionnaireController.listQuestionnaire(QuestionnaireController.java:121)
20:05:50,565 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
20:05:50,565 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
20:05:50,565 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
20:05:50,565 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:597)
20:05:50,565 ERROR [STDERR] at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
20:05:50,565 ERROR [STDERR] at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
20:05:50,565 ERROR [STDERR] at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
20:05:50,565 ERROR [STDERR] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
20:05:50,566 ERROR [STDERR] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
20:05:50,566 ERROR [STDERR] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
20:05:50,566 ERROR [STDERR] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
20:05:50,566 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
20:05:50,566 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
20:05:50,566 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
20:05:50,566 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
20:05:50,566 ERROR [STDERR] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
20:05:50,566 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
20:05:50,566 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
20:05:50,566 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
20:05:50,566 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
20:05:50,566 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
20:05:50,566 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
20:05:50,567 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
20:05:50,567 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
20:05:50,567 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
20:05:50,567 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
20:05:50,567 ERROR [STDERR] at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
20:05:50,567 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
20:05:50,567 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
20:05:50,567 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
20:05:50,567 ERROR [STDERR] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
20:05:50,567 ERROR [STDERR] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
20:05:50,567 ERROR [STDERR] at java.lang.Thread.run(Thread.java:680)
但是,如果我在DAO中尝试使用以下查询,它可以正常工作
String queryString1 = "select new com.apple.ist.cm.ruleadmin.model.SearchQuestionnaire(qc.questionCategoryID,qc.questionCategoryName,qn.questionReferenceString, question)"
+" from Questionnaire qn,QuestionCategory qc where qn.questionCategoryID = qc.questionCategoryID";
请帮我解决“NullPointerException”。
答案 0 :(得分:1)
我很惊讶第二个查询有效,因为查询调用的构造函数的参数与SearchQuestionnaire的任何构造函数都不匹配。确保查询使用与该类的构造函数之一相同的数字和相同类型的参数。
BTW,SearchQuestionnaire的第二个构造函数被破坏了。它需要两个参数并用自己初始化大多数属性。 顺便说一下,你的实体的设计似乎有问题,因为你不应该自己加入ID。相反,您应该在实体之间建立关联并加入这些关联。例如,Question实体不应具有类别ID。它应该与QuestionCategory有一个ManyToOne关联。答案 1 :(得分:0)
用包装类Long 替换原语long,或者确保您的查询返回的所有项目都不为空。
原因是long是一个原语,不能取NULL值。使用hibernate,最好将Wrapper类用于表中所有可为空的字段。