自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

對Hibernate sum函數(shù)的使用之分析

開發(fā)
本文向您介紹使用Hibernate的sum函數(shù)進行數(shù)據(jù)的統(tǒng)計時,出現(xiàn)的錯誤代碼以及相關(guān)的解決辦法。

在使用Hibernate的sum函數(shù)進行數(shù)據(jù)的統(tǒng)計時,出現(xiàn)一個錯誤代碼:

Java代碼

String sql = "select SUM(nf.fee) from CFee as nf where   nf.adminAccount='testaccount' ";
public long getListSqlCountsLong(String sql) {
beginTransaction();
List li = getSession().createQuery(sql).list();
if (li == null || li.isEmpty()) {
return 0;
} else {return ((Integer) li.get(0)).longValue();
}
}
String sql = "select SUM(nf.fee) from CFee as nf where   nf.adminAccount='testaccount' ";
public long getListSqlCountsLong(String sql) {
beginTransaction();
List li = getSession().createQuery(sql).list();
if (li == null || li.isEmpty()) {
return 0;
} else {return ((Integer) li.get(0)).longValue();
}
}

這樣使用報null錯誤.
List的size明明等于1,但li.get(0)還是為空.(數(shù)據(jù)庫中查詢的賬號sum本來就為null??可能是.)
解決方法:

Java代碼

String sql = "select SUM(nf.fee) from CFee as nf where   nf.adminAccount='testaccount' ";
public long getListSqlCountsLong(String sql) {
beginTransaction();
List li = getSession().createQuery(sql).list();
if (li == null || li.isEmpty()) {
return 0;
} else {
if (li.get(0) == null) {
return 0;
}
return ((Integer) li.get(0)).longValue();
}
}
String sql = "select SUM(nf.fee) from CFee as nf where   nf.adminAccount='testaccount' ";
public long getListSqlCountsLong(String sql) {
beginTransaction();
List li = getSession().createQuery(sql).list();
if (li == null || li.isEmpty()) {
return 0;
} else {
if (li.get(0) == null) {
return 0;
}
return ((Integer) li.get(0)).longValue();
}
}
解決方法很簡單,就是增加一個判斷就可以了,如果li.get(0)為空,則返回0,不為空,返回值. 這樣就可以解決Hibernate sum函數(shù)使用出錯的問題。

【編輯推薦】

  1. 選擇EJB3.0,不再需要Spring+Hibernate
  2. Hibernate一對多關(guān)系的處理
  3. Struts與Hibernate的***結(jié)合方案
  4. 淺談Struts分頁中的Hibernate如何實現(xiàn)
責任編輯:張攀 來源: sech.javaeye.com
相關(guān)推薦

2009-06-18 14:51:12

Hibernate緩存Hibernate

2009-06-12 15:32:01

Hibernate H

2009-06-12 15:05:03

cascadeHibernate

2014-01-03 13:27:33

PostgreSQL

2015-07-13 09:56:37

2009-06-16 14:36:54

Hibernate繼承

2009-09-22 13:14:29

Hibernate gHibernate l

2017-04-24 09:20:05

Spark分析分區(qū)器

2009-09-23 16:39:51

Hibernate s

2009-09-24 12:50:23

Hibernate F

2009-09-22 13:35:04

Hibernate A

2009-09-24 09:35:47

Hibernate插入

2009-09-28 15:38:12

Hibernate P

2009-09-23 13:33:51

Hibernate屬性

2009-09-23 10:28:16

Hibernate映像

2009-09-22 14:23:37

Hibernate S

2009-09-21 18:00:49

Hibernate X

2009-09-22 17:47:03

Hibernate s

2009-09-22 11:30:57

2009-06-29 09:00:42

Hibernate的Q
點贊
收藏

51CTO技術(shù)棧公眾號