hibernateTemple拡張ページング

2399 ワード




	/**
	 *   HQL          
	 * offset         
	 * pageSize           
	 * @return         
	 */
	public List findByPage(final String hql, final int offset, final int pageSize)
	throws Exception
	{
		List list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				List result = session.createQuery(hql).setFirstResult(offset)
								.setMaxResults(pageSize)
								.list();
				return result;
			}
		});
		return list;
	}
	
	/**
	 *   HQL          
	 * value   HQL         , value       
	 * offset         
	 * pageSize           
	 * @return         
	 */
	public List findByPage(final String hql, final Object value,  final int offset, final int pageSize)
	throws Exception
	{
//System.out.println("PageDaoHibernate.findByPage()");
		List list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				List result = session.createQuery(hql).setFirstResult(offset)
								.setParameter(0, value)
								.setMaxResults(pageSize)
								.list();
				return result;
			}
		});
		return list;
	}
	
	/**
	 *   HQL          
	 * values   HQL         , values         
	 * offset         
	 * pageSize           
	 * @return         
	 */
	public List findByPage(final String hql, final Object[] values,  final int offset, final int pageSize)
	throws Exception
	{
		List list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query = session.createQuery(hql);
				for (int i = 0; i < values.length; i++) {
					query.setParameter(i, values[i]);
				}
				List result = query.setFirstResult(offset)
								.setMaxResults(pageSize)
								.list();
				return result;
			}
		});
		return list;
	}