异常org.hibernate.QueryException: could not resolve property的原因
程序员文章站
2022-06-06 13:18:33
...
异常消息如下所示:
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.<init>(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")。
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.<init>(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")。