hibernate sqlQuery mysql別名問題

來源:互聯網
上載者:User

標籤: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別名問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.