ITeye博客

hibernate 随机 查询

conkeyn · 2009-04-03 21:36 · Java/Hibernate · Hibernate · Markdown

/**
	 * 随机取出N条记录
	 * 
	 * @param filetype
	 * @param position
	 * @param dialog_box_type
	 * @param total
	 *            记录总数
	 * @param count
	 *            要取出几条
	 * @return
	 */
	@Override
	public List<Adv> getAdvs(String filetype, String position,
			String dialog_box_type, int total, int count) {
		String hql = "from Adv as adv where adv.id is not null ";
		if (StringUtils.isNotBlank(filetype)) {
			hql += " and adv.filetype ='" + filetype + "' ";
		}
		if (StringUtils.isNotBlank(position)) {
			hql += " and adv.position ='" + position + "' ";
		}
		if (StringUtils.isNotBlank(dialog_box_type)) {
			hql += " and adv.type.id ='" + dialog_box_type + "' ";
		}
		int first = total - count;
		first = first >= 0 ? first : 0;
		first = (int) Math.floor(Math.random() * first);
		return getSession().createQuery(hql).setFirstResult(first)
				.setMaxResults(count).list();
	}

评论(1)

  1. #1 楼 · wang363714266 · 2009-10-13 16:53
    alert("hello!")
    

评论功能维护中,当前仅展示历史评论。