异常org.hibernate.QueryException: could not resolve property的原因 解决办法

异常org.hibernate.QueryException: could not resolve property的原因 解决办法

异常消息如下所示:

org.hibernate.QueryException: could not resolve property: task_no of: edu.cust.entity.Task
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.BasicEntityPersister.getSubclassPropertyTableNumber

(BasicEntityPersister.java:1111)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
at org.hibernate.persister.entity.BasicEntityPersister.toColumns(BasicEntityPersister.java:1086)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:403)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection

(CriteriaQueryTranslator.java:369)
at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:42)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:314)
at org.hibernate.loader.criteria.CriteriaLoader.(CriteriaLoader.java:92)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1303)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
at edu.cust.dao.TaskDao.searchTask(TaskDao.java:129)
at edu.cust.struts.action.SearchTaskFuelAction.execute(SearchTaskFuelAction.java:69)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)

这里,关键是“task_no of: edu.cust.entity.Task”,task_no为数据库中表的字段,异常时说在查询的时候无法解析这个字段

,也就是说这个字段是错误的。

由于Eclipse能够进行Hibernate映射,并自动生成持久化类和对应的配置文件,对于类似于task_no中带有下划线的字段,则自动

将下划线去掉,task_no在持久化类中对应属性为taskNo,所以,在Dao中编写查询的方法中,必须写成

criteria.add(Restrictions.eq("taskNo",task.getTaskNo()));

这里eq()方法的第一个参数对应于持久化类中的属性名称,如果使用下面:

criteria.add(Restrictions.eq("task_no",task.getTaskNo()));

就可能发生上面的异常,千万要小心。

一般地,如果涉及到属性类型无法解析的异常,可能出现问题的地方有:

数据库字段与持久化类映射文件,以及持久化类文件中属性名称或者类型可能不相匹配;

持久化类映射文件中属性类型可能有问题,比如,如果使用Java类型,注意大写(如type="java.lang.String"),如果使用

Hibernate类型,使用小写(如type="string")。
 

====================================================================

经检查,我的代码中使用了
 String str = "select oi from Orderitem oi,Order o where oi.user='" + user + "' and oi.onCart=0 and

o.orderId=oi.order and o.orderId=" + orderId
                + " and (o.orderState='" + Order.GOODS_SENT+ "' or o.orderState='"+Order.GOODS_RECEIVED+"')";

其中在,Orderitem中并无orderId这个属性,只有order属性。而我之前误以为属性只要跟数据库里的一致即可,孰不知这里是面

向对象的,即应该以pojo中的属性名为准。

怪不得之前老是提示:


org.hibernate.QueryException: could not resolve property: orderId of: shoppingSystemOnline.persistence.Orderitem

[select oi from shoppingSystemOnline.persistence.Orderitem oi,shoppingSystemOnline.persistence.Order o where

oi.user='mike' and oi.onCart=0 and o.orderId=oi.orderId and o.orderId=12 and (o.orderState='3' or

o.orderState='4')]
 org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44)
 org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:38)
 org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1358)
 org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:279)
 org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:386)
 org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:566)
 org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:241)
 org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:188)
 org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:94)
 org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:90)
 org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:728)
 org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1216)
 org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4041)
 org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3528)
 org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1762)
 org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1690)
 org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1687)
 org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1687)
 org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:776)
 org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577)
 org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
 org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
 org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
 org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
 org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
 org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:77)
 org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:56)
 org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
 org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
 org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
 org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
 shoppingSystemOnline.persistence.HibernateUtil.listResults(HibernateUtil.java:175)
 shoppingSystemOnline.control.frontModule.userFunction.UserAskMoneyReturn.doUserAskMoneyReturn

(UserAskMoneyReturn.java:67)
 shoppingSystemOnline.control.frontModule.userFunction.UserAskMoneyReturn.execute

(UserAskMoneyReturn.java:53)
 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 java.lang.reflect.Method.invoke(Method.java:597)
 com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
 com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
 com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept

(DefaultWorkflowInterceptor.java:176)
 com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
 org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept

(AnnotationValidationInterceptor.java:68)
 com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept

(ConversionErrorInterceptor.java:133)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
 com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
 com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept

(StaticParametersInterceptor.java:190)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept

(ScopedModelDrivenInterceptor.java:141)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)
 com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept

(ExceptionMappingInterceptor.java:187)
 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
 org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
 org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
 org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter

(StrutsPrepareAndExecuteFilter.java:91)
 org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

 

 

另外,貌似在hql中自然连接、内连接、外连接的语法跟sql中不大一样啊,昨晚今天弄了很久还是没搞懂怎么弄才对。最后只好

用最笨的条件查询、多表联合查询来解决了。。。。。。。

你可能感兴趣的:(hibernate,exception,hql,hibernate3)