我无法进行hibernate sql查询

时间:2016-10-05 03:46:52

标签: sql postgresql hibernate

嗨,我是一个菜鸟程序员。

我正在学习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和hibernate 5.0。*

请帮帮我

1 个答案:

答案 0 :(得分:0)

1)而不是createQuery使用createSQLQuery,如下所示:

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运算符。