hibernate 或者Jpa 报错 java.sql.SQLException: Column ‘列名’ not found 解决

今天遇到一个很奇怪的问题,就是某一段SQL 在我的电脑里面执行的时候报错了。虽然这段SQL是原生的复杂SQL。

但是在 测试环境那边没有问题啊。而且我看了一下SVN的记录,发现当前版本也没有人改动啊!!怎么就出现了这个问题?

先上异常把

jpa Column 'totalWeight' not found.

反正 出现来堆的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 那边执行了一下, 发现没有问题啊!!!

也就是说,问题很可能是 在JPA解析结果集的时候报错的。可是看起来怎么会出现问题呢?

还找不到字段? 而且我的是原生 SQL啊,没有使用 HQL的

然后我百度了一下 

参考 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  带上别名就报错了

 

 

 

 

转载于:https://my.oschina.net/ouminzy/blog/2997097

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值