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();
	}

}
 

你可能感兴趣的:(HibernateTemplate Callback)