org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query

本文详细分析了一个在使用Spring框架进行数据库查询时遇到的InvalidDataAccessResourceUsageException错误。错误源于SQL语句中字符串字段未正确添加单引号。文章提供了从原始代码到修正后的代码的对比,并解释了如何避免此类问题,以确保数据库操作的正确性和效率。

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select count(*) as col_0_0_ from NOTICES notices0_ where notices0_.SENDUNIT=北京市药品监督管理局大兴分局机关 order by notices0_.NOTICEDATE desc]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:629)
... (省略一大堆异常信息)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query


-- 错误原因:
1. 原代码: String sql = "from Notices n where n.sendunit = " + sendunit + "order by n.noticedate desc";
2. 现代码: String sql = "from Notices n where n.sendunit ='" + sendunit + "' order by n.noticedate desc";
3. 这个字段是String(varchar2)类型, 没有加上单引号

context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [SELECT DISTINCT line.* FROM C_LINE line INNER JOIN ( SELECT @a :=- 1, ? optDeptType, ? optid, ? sAreaId, ? eAreaId, ? claTraType, ? lineIds, ? platform ) param LEFT JOIN C_LINE_OPERATING_COMPANY lo ON lo.LINE_ID = line.ID LEFT JOIN R_OPERATING_COMPANY oc ON oc.ID = lo.OC_ID LEFT JOIN OPERATOR_LINE opli ON opli.OPERATOR_ID = param.optid AND opli.LINE_ID = line.ID WHERE ( SELECT ( param.optDeptType = 4 || param.optDeptType = 3 && line.PLATFORM = ext.PLATFORM || ( param.optDeptType = 1 && ( FIND_IN_SET( oc.DEPT_ID, IF ( @a =- 1, @a := FIND_CHILDREN_OF_DEPT ( dept.ID ), @a ) ) && opli.OPERATOR_ID IS NULL || opli.IS_INVERSE = 0 ) ) ) FROM U_OPERATOR opt LEFT JOIN U_DEPT dept ON dept.ID = opt.DEPT_ID LEFT JOIN DEPT_EXTEND ext ON ext.ID = dept.ID WHERE opt.id = param.optid ) AND IF ( param.sAreaId = - 1, TRUE, line.START_AREA_ID = param.sAreaId ) AND IF ( param.eAreaId = - 1, TRUE, line.END_AREA_ID = param.eAreaId ) AND IF ( param.claTraType = - 1, TRUE, param.claTraType = line.type ) AND IF ( param.lineIds = '-1', TRUE, FIND_IN_SET(line.ID, param.lineIds) ) AND IF ( param.platform = - 1, TRUE, param.platform = line.PLATFORM ) ORDER BY line.SORT DESC]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause java.sql.SQLException: Column 'LEVEL' not found.
最新发布
09-29
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值