hibernate的常见错误
1.查询记录总数
错误:HQL:select count(1) from ....
正确:HQL:select count(*) from ....
2.Criteria的同一个条件不能复用,
例如
错误:
Criteria criteria = session.createCriteria(A.class); criteria.add(Restrictions.eq("field1","xx"); criteria.list(); criteria.add(Restrictions.eq("field1","yy");//出错 criteria.list();
正确:
Criteria criteria = session.createCriteria(A.class); criteria.add(Restrictions.eq("field1","xx"); criteria.list(); criteria=session.createCriteria(A.class); criteria.add(Restrictions.eq("field1","yy"); criteria.list();
3.criteria的rowcount返回的是Integer
错误:
Criteria criteria = XXDAO.getCriteria(XX.class); criteria.setProjection(Projections.rowCount()); int count = ((Long)procriteria.uniqueResult()).intValue();//错误
正确:
Criteria criteria = XXDAO.getCriteria(XX.class); criteria.setProjection(Projections.rowCount()); int count = ((Integer)procriteria.uniqueResult()).intValue();//正确
4.HQL对聚合函数count,sum返回的类型却是Long,这点要注意和3区分开。
相关推荐
LetonLIU 2020-03-28
东方咖啡屋 2020-02-14
LetonLIU 2019-12-23
qinkang 2019-11-09
surpassdream 2011-12-11
张浩BIT 2011-09-27
Franklyn 2017-01-01
hanyueqi 2019-06-30
康慧欣 2015-09-08
minerk 2019-06-30
逆流而上的鲤鱼 2013-07-19
xhljames 2012-12-07
frank0 2012-08-14
longxx 2012-07-09
阳光丶不锈 2012-07-09
zhongranxu 2019-06-28
Lendor 2012-04-25