标签:hibernate sqlquery mysql别名问题
这两天,在写一些订单数据的查询展示(因为有些数据想在sql中直接算出)提高效率。所以用了自己拼装的 hql
之前是的sql如下:
妥投/退货的SQL
SELECT o.id ,o.sn, o.community_id , o.seller_id , o.status , o.pay_rate ,o.payment_id,o.payment_profit,o.cwy_profit, o.amount, o.delivery_fee ,o.bank_pay , o.completed_date ,o.create_time , o.red_packet_pay , o.coupon_pay ,o.cwy_profit, (o.pay_rate * o.bank_pay / 100) as poundage , (o.cwy_profit * o.bank_pay) as profit , (o.payment_profit * o.bank_pay / 100) as pay_profit , c.name as comm_name , s.name as shop_name , s.checkout_days , s.deduct , ri.shop_id as red_shop_id , ri.red_money as ri_money , qi.shop_id as coupon_shop_id , qi.coupon_money as qi_money, d.id as diff_fee_id,d.status as diff_status,d.diff_fee as fee,d.balance_id FROM `order` o LEFT JOIN diff_fee d ON d.order_id = o.id LEFT JOIN community c ON o.community_id = c.id LEFT JOIN shop s ON o.seller_id = s.id LEFT JOIN red_receives r ON o.red_recevices_id = r.id LEFT JOIN red_packet_infos ri ON r.red_id = ri.id LEFT JOIN coupon_receives q ON o.coupon_recevices_id = q.id LEFT JOIN coupon_infos qi ON q.coupon_id = qi.id WHERE o.community_id > 0 AND o.payment_id != 0 AND o.status IN ( 4,5,7 ) AND UNIX_TIMESTAMP( o.completed_date ) >= :start_time AND UNIX_TIMESTAMP( o.completed_date ) <= :end_time AND o.seller_id = :seller_id ORDER BY completed_date DESC
但是,一直报错,说
1] (JDBCExceptionReporter.java:234) - Column ‘shop_id ‘ not found.
后面我吧 有关 shop_id的列删除了,继续报下个列 coupon_money 找不到。很郁闷,各种可能性都想过了,也叫同事帮看了,都找不到原因。后面列去家,发现是 用了 as 去另外起别名引起的,后面就改成了下面的语句,就行了。
sql="SELECT o.id ,o.sn, o.community_id , o.seller_id , o.status , o.pay_rate ,o.payment_id,o.payment_profit,o.cwy_profit, o.amount, o.delivery_fee ,o.bank_pay , UNIX_TIMESTAMP(o.completed_date) ,o.create_time , o.red_packet_pay , o.coupon_pay ,o.cwy_profit, (o.pay_rate * o.bank_pay / 100) as poundage , (o.cwy_profit * o.bank_pay) as profit , (o.payment_profit * o.bank_pay / 100) as pay_profit , c.name , s.name , s.checkout_days , s.deduct , ri.shop_id , ri.red_money , qi.shop_id , qi.coupon_money , d.id ,d.status ,d.diff_fee ,d.balance_id FROM `order` o LEFT JOIN diff_fee d ON d.order_id = o.id LEFT JOIN community c ON o.community_id = c.id LEFT JOIN shop s ON o.seller_id = s.id LEFT JOIN red_receives r ON o.red_recevices_id = r.id LEFT JOIN red_packet_infos ri ON r.red_id = ri.id LEFT JOIN coupon_receives q ON o.coupon_recevices_id = q.id LEFT JOIN coupon_infos qi ON q.coupon_id = qi.id WHERE o.community_id > 0 AND o.payment_id != 0 AND o.status =10 AND UNIX_TIMESTAMP( o.completed_date ) >= "+balance.getStartTime()+" AND UNIX_TIMESTAMP( o.completed_date ) <= "+balance.getEndTime()+" AND o.seller_id = "+form.getShopId()+" ORDER BY completed_date DESC";
看到网友还有一种解决办法,不知道行不行,我没试过,也记录一下吧
解救方法:
困扰了我很久 ,找了很多资料,问题很简单,就是驱动不支持别名的问题,这里记录下,为后来人造桥!!! 在连接mysql数据库的时候添加 useOldAliasMetadataBehavior=true 这个参数!!!!
jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/db?useUnicode\=true&characterEncoding\=utf8&useOldAliasMetadataBehavior=true
标签:hibernate sqlquery mysql别名问题
原文地址:http://1008610086.blog.51cto.com/4995677/1768254