今天遇到一个很奇怪的问题,就是某一段SQL 在个人电脑里面执行的时候报错了。虽然这段SQL是原生的复杂SQL。java
可是在 测试环境那边没有问题啊。并且我看了一下SVN的记录,发现当前版本也没有人改动啊!!怎么就出现了这个问题?mysql
先上异常把spring
jpa Column 'totalWeight' not found.
反正 出现来堆的SQL最好爆了这个。sql
由于这个是 分页查询,发现是在 查询总记录数的时候,没有报错。报错的是接下来的 limit分页查询记录。函数
由于个人本地项目配置了 p6spy 所以能够看到发给MYSQL的 原生SQL测试
SELECT a.id, a.createtime,a.goods_amount, a.order_id,a.order_status,a.totalPrice,a.ziti_name, a.ziti_mobile,b.store_name, b.store_telephone, b.store_ower,c.userName, d.en_name,d.en_ceo,d.en_address,w.total_weight as total_goods_weight, a.borrow_status FROM gwqmshop_order a left join gwqmshop_store b on b.id=a.store_id left join gwqmshop_user c on c.id =a.user_id left join (SELECT of_id, SUM(t.goods_weight*t.count) as total_weight FROM gwqmshop_goods_shopcart t group by t.of_id) w on w.of_id=a.id left join gwqmshop_user_enterprise d on d.user_id=a.user_id where 1=1 and a.disabled=0 order by a.createtime desc limit 12;
就是上面这样的。而后我把它 拿过去mysql 那边执行了一下, 发现没有问题啊!!!url
也就是说,问题极可能是 在JPA解析结果集的时候报错的。但是看起来怎么会出现问题呢?.net
还找不到字段? 并且个人是原生 SQL啊,没有使用 HQL的hibernate
而后我百度了一下 unix
参考 https://blog.csdn.net/love_moon821/article/details/80015851
说是 不支持 别名? 确实有了 别名。
我开始是由于是 left join 级联的别名问题
(SELECT of_id,
SUM(t.goods_weight*t.count),t.goods_weight,t.count as total_weight FROM gwqmshop_goods_shopcart t
group by
t.of_id)
但是 这个是 子表,并且是 分组的,必须有啊!!!
并且必须这些写啊!!! 因而我试着把上面的
w.total_weight as total_goods_weight 这个 别名去掉
去掉以后, 跑起来项目,结果没有问题了。
也是服了,我以为JPA对于HQL不能够有别名还能够理解,可是是原生SQL 仍是不行。
并且是 解析结果集的时候问题,通常出现SQL异常不是 MYSQL那边出错致使的吗。
算是 JPA那些的坑吧
相似的错误
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select oi.texture as goods_outurl, sum(oi.num) as total_count, sum(oi.total_weight) as total_weight, sum(o.total_price) as total_amount from v_all_order o join v_all_order_item oi on oi.order_id=o.id where o.user_id = 777 and o.order_status>0 and unix_timestamp(o.createtime) >= unix_timestamp('2019-01-01 00:00:00') and unix_timestamp(o.createtime) <= unix_timestamp('2019-12-31 23:59:59') group by oi.texture order by oi.texture ]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:635) Caused by: org.hibernate.exception.SQLGrammarException: could not execute query at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.loader.Loader.doList(Loader.java:2231) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125) at org.hibernate.loader.Loader.list(Loader.java:2120) at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:312) at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1722) at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165) at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:175) at com.gwqm.base.basedao.GenericEntityDao$7.doInJpa(GenericEntityDao.java:293) at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:187) ... 127 more Caused by: java.sql.SQLException: Column 'texture' not found.
可是我发现了一个问题,就是 若是 使用了SQL 函数,带上别名是 支持的,不会报错
select oi.texture , sum(oi.num) as total_count, sum(oi.total_weight) as total_weight, sum(o.total_price) as total_amount from v_all_order o join v_all_order_item oi on oi.order_id=o.id where o.user_id = 777 and o.order_status>0 and unix_timestamp(o.createtime) >= unix_timestamp('2019-01-01 00:00:00') and unix_timestamp(o.createtime) <= unix_timestamp('2019-12-31 23:59:59') group by oi.texture order by oi.texture ;
好比 带上函数 sum ,count 带上别名是没有报错的,
可是 texture 带上别名就报错了