2016-10-05 105 views
-1

嗨,我是一個noob程序員。我不能讓休眠sql查詢

我正在學習Hibernate。而我對

SELECT COUNT(*) FROM zipcode a 
WHERE (ISNULL(:sido , '') = '' OR a.sido = :sido) AND 
(ISNULL(:sigungu , '') = '' OR a.sigungu = :sigungu) AND 
    (ISNULL(:eupmyun , '') = '' OR a.eupmyun = :eupmyun) AND 
    (ISNULL(:roadName , '') = '' OR a.road_name = :roadName) AND 
    (ISNULL(:ri , '') = '' OR a.ri = :ri) 

沮喪,我不能讓這個sql查詢冬眠....

Number num = (Number) session() 
       .createQuery(
         "SELECT COUNT(*) FROM ZipCode a WHERE (ISNULL(:sido , '') = '' OR a.sido = :sido) AND (ISNULL(:sigungu , '') = '' OR a.sigungu = :sigungu) AND (ISNULL(:eupmyun , '') = '' OR a.eupmyun = :eupmyun) AND(ISNULL(:roadName , '') = '' OR a.roadName = :roadName) AND(ISNULL(:ri , '') = '' OR a.ri = :ri) ") 
       .setParameter("sido", sido).setParameter("sigungu", sigungu).setParameter("eupmyun", eupmyun).setParameter("roadName", roadName).setParameter("ri", ri) 
       .list(); 

我試試這個SQL ... 我使用PostgreSQL 9.4和5.0冬眠。 *

請幫我

+0

PostgreSQL有一個錯誤,他們不認爲這是一個錯誤,NULL有100多種數據類型之一,但不能在大多數數據類型之間進行轉換。儘可能避免使用'setParameter(something,null)',除非您計劃切換數據庫。 – coladict

回答

0

1)而不是createQuery使用createSQLQuery在Bel流量:

Query query = session.createSQLQuery("SELECT COUNT(*) FROM zipcode a 
         WHERE (ISNULL(:sido , '') = '' OR a.sido = :sido) AND 
        (ISNULL(:sigungu , '') = '' OR a.sigungu = :sigungu) AND 
        (ISNULL(:eupmyun , '') = '' OR a.eupmyun = :eupmyun) AND   
        (ISNULL(:roadName , '') = '' OR a.road_name = :roadName) AND 
        (ISNULL(:ri , '') = '' OR a.ri = :ri) "); 

Number number =(Number)query.list().get(0); 

createQuery - 是一個HQL查詢,而createSQLQuery是土生土長的數據庫查詢。

注意:請確保查詢在一行中,否則您需要添加('+')concat運算符。