Hibernate-hibernate 中 Long 与 int 的转换
来源:互联网 发布:java线程池使用demo 编辑:程序博客网 时间:2024/06/13 11:59
hibernate 中使用聚合函数 count 查询记录数时,返回值类型为 Long ,若强行转换则会出现 java.lang.ClassCastException 异常,
正确写法应使用 Number 类型作为过渡则不会出错。
错误写法:
@Overridepublic int findCountByCid(Integer cid) {String hql = "SELECT count(*) from Product p WHERE p.categorySecond.category.cid = ?";Query query = getSession().createQuery(hql).setInteger(0, cid);List<Integer> list = query.list();//返回值类型应为 Long 强行转换 Integer int result = list.get(0);//调用将会出现异常return result;}
更正后:
@Overridepublic int findCountByCid(Integer cid) {String hql = "SELECT count(*) from Product p WHERE p.categorySecond.category.cid = ?";Query query = getSession().createQuery(hql).setInteger(0, cid);List<Long> list = query.list();//修正部分int result = ((Number)list.get(0)).intValue();//修正部分return result;}
0 0
- Hibernate-hibernate 中 Long 与 int 的转换
- int与long long之间的转换
- 整型与指针的相互转换(int<->int*,long<->long*)
- long(Long)与int(Integer)之间的转换
- JAVA中long与int的转化
- java中int与byte,以及long与byte之间的转换
- java中int、long与byte数组相互转换
- Java 语言中 short 与 byte[]之间的转换,int、long 类似。
- java中byte数组与int,long,short间的转换
- java中long,int,short与byte数组之间的转换
- java中long,int,short与byte数组之间的转换
- java中byte数组与int,long,short,float,char之间的转换
- java中byte数组与int,long,short间的转换 .
- java中byte数组与int,long,short间的转换
- java中byte数组与int,long,short,float,char之间的转换
- java中byte数组与int,long,short间的转换
- java中long,int,short与byte数组之间的转换
- Java中byte数组与short,int,char,long,float,double之间的转换
- 初识-IIS(互联网信息服务)
- sql map
- 《CSS层叠样式表》-----第四课
- response返回的json数据的list对象返回错误
- Shell中的正则表达式
- Hibernate-hibernate 中 Long 与 int 的转换
- 最小m段和
- linux c之htpasswd
- Git@OSC通过Eclipse生成SSH keys RSA公钥私钥
- 通配符和动态方法调用Action
- 笔记:JDBC(Java DataBace Connectivity)
- Linux CentOS 安装Java环境
- jQuery对象小结
- 泛型,反射,公共dao层的使用