08-10
07
java.lang.IllegalArgumentException: n must be positive
作者:Java伴侣 日期:2008-10-07
复制内容到剪贴板 程序代码
public List getMaxRsRandArticle(String hql, int num) {
List list = new ArrayList();
Transaction tx = null;
Session session = HibernateUtil.getSession();
try {
tx = session.beginTransaction();
Query query = session.createQuery(hql);
log.info("随机HQL:"+hql);
int size = query.list().size();
Random r = new Random();
query.setMaxResults(10);
// query.setFirstResult(r.nextInt(size - 10) + 1);
query.setFirstResult(r.nextInt(Math.abs(size - 10)) + 1);
list = (List) query.setMaxResults(num).list();
tx.commit();
} catch (HibernateException ex) {
ex.printStackTrace();
if (tx != null)
HibernateUtil.rollbackTransaction(tx);
} finally {
if (session != null) {
HibernateUtil.closeSession(session);
}
}
return list;
}
List list = new ArrayList();
Transaction tx = null;
Session session = HibernateUtil.getSession();
try {
tx = session.beginTransaction();
Query query = session.createQuery(hql);
log.info("随机HQL:"+hql);
int size = query.list().size();
Random r = new Random();
query.setMaxResults(10);
// query.setFirstResult(r.nextInt(size - 10) + 1);
query.setFirstResult(r.nextInt(Math.abs(size - 10)) + 1);
list = (List) query.setMaxResults(num).list();
tx.commit();
} catch (HibernateException ex) {
ex.printStackTrace();
if (tx != null)
HibernateUtil.rollbackTransaction(tx);
} finally {
if (session != null) {
HibernateUtil.closeSession(session);
}
}
return list;
}
*红色的注明,当list取值为空,size为0,那么就会为负数。解决办法,使用绝对值
评论: 0 | 引用: 0 | 查看次数: 1399
发表评论