HibernateTemplate Callback

package com.bee.common.hibernate.callback;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

/**
 * 分页查询
 * 
 * @author Bee
 * 
 * @param <T>
 */
public class PagingHibernateCallback<T> implements HibernateCallback<T> {

	String hql;
	int first;
	int length;

	public PagingHibernateCallback(String hql, int first, int length) {
		this.hql = hql;
		this.first = first;
		this.length = length;
	}

	@SuppressWarnings("unchecked")
	public T doInHibernate(Session session) throws HibernateException,
			SQLException {
		Query query = session.createQuery(hql);
		query.setFirstResult(first);
		query.setMaxResults(length);
		return (T) query.list();
	}

}
 

猜你喜欢

转载自387424-student-sina-com.iteye.com/blog/1568635