自己写的简单版HibernateTemplate
HibernateTemplate :
import infoair.fims.common.HibernateSessionFactory;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class HibernateTemplate {
?? ?public Session getSession() {
?? ??? ?return HibernateSessionFactory.getSession();
?? ?}
?? ?/**
?? ? * 执行回调方法。
?? ? *
?? ? * @param action
?? ? * @return
?? ? * @throws DataAccessException
?? ? */
?? ?public Object execute(HibernateCallback action) throws DataAccessException {
?? ??? ?Session session = null;
?? ??? ?Transaction tran = null;
?? ??? ?try {
?? ??? ??? ?session = getSession();
?? ??? ??? ?tran = session.beginTransaction();
?? ??? ??? ?Object result = action.doInHibernate(session);
?? ??? ??? ?tran.commit();
?? ??? ??? ?return result;
?? ??? ?} catch (HibernateException ex) {
?? ??? ??? ?if (tran != null) {
?? ??? ??? ??? ?tran.rollback();
?? ??? ??? ?}
?? ??? ??? ?ex.printStackTrace();
?? ??? ??? ?throw new DataAccessException("hibernate error");
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?if (tran != null) {
?? ??? ??? ??? ?tran.rollback();
?? ??? ??? ?}
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?throw new DataAccessException("sql error");
?? ??? ?} catch (RuntimeException ex) {
?? ??? ??? ?if (tran != null) {
?? ??? ??? ??? ?tran.rollback();
?? ??? ??? ?}
?? ??? ??? ?ex.printStackTrace();
?? ??? ??? ?throw ex;
?? ??? ?} finally {
?? ??? ??? ?HibernateSessionFactory.closeSession();
?? ??? ?}
?? ?}
?? ?/**
?? ? * 根据主键,load一个对象.
?? ? *
?? ? * @param entityClass
?? ? * @param id
?? ? * @return
?? ? * @throws DataAccessException
?? ? */
?? ?public Object load(final Class entityClass, final Serializable id)
?? ??? ??? ?throws DataAccessException {
?? ??? ?return execute(new HibernateCallback() {
?? ??? ??? ?public Object doInHibernate(Session session)
?? ??? ??? ??? ??? ?throws HibernateException {
?? ??? ??? ??? ?return session.load(entityClass, id);
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?/**
?? ? * 根据主键,get一个对象.
?? ? *
?? ? * @param entityClass
?? ? * @param id
?? ? * @return
?? ? * @throws DataAccessException
?? ? */
?? ?public Object get(final Class entityClass, final Serializable id)
?? ??? ??? ?throws DataAccessException {
?? ??? ?return execute(new HibernateCallback() {
?? ??? ??? ?public Object doInHibernate(Session session)
?? ??? ??? ??? ??? ?throws HibernateException {
?? ??? ??? ??? ?return session.get(entityClass, id);
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?/**
?? ? * 返回entityClass的所有对象的List
?? ? *
?? ? * @param entityClass
?? ? * @param id
?? ? * @return
?? ? * @throws DataAccessException
?? ? */
?? ?public List findAll(final Class clazz) throws DataAccessException {
?? ??? ?return (List) execute(new HibernateCallback() {
?? ??? ??? ?public Object doInHibernate(Session session)
?? ??? ??? ??? ??? ?throws HibernateException, SQLException {
?? ??? ??? ??? ?Query query = session.createQuery("from " + clazz.getName());
?? ??? ??? ??? ?return query.list();
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?/**
?? ? * 传入hql 返回结果List
?? ? *
?? ? * @param queryHql
?? ? * @return
?? ? * @throws DataAccessException
?? ? */
?? ?public List findAll(final String queryHql) throws DataAccessException {
?? ??? ?return (List) execute(new HibernateCallback() {
?? ??? ??? ?public Object doInHibernate(Session session)
?? ??? ??? ??? ??? ?throws HibernateException, SQLException {
?? ??? ??? ??? ?Query query = session.createQuery(queryHql);
?? ??? ??? ??? ?return query.list();
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?/**
?? ? * 分页查找
?? ? *
?? ? * @param clazz
?? ? * @param firstResult
?? ? * @param maxResults
?? ? * @return
?? ? * @throws DataAccessException
?? ? */
?? ?public List find(final Class clazz, final int firstResult,
?? ??? ??? ?final int maxResults) throws DataAccessException {
?? ??? ?return (List) execute(new HibernateCallback() {
?? ??? ??? ?public Object doInHibernate(Session session)
?? ??? ??? ??? ??? ?throws HibernateException, SQLException {
?? ??? ??? ??? ?Query query = session.createQuery("from" + clazz.getName())
?? ??? ??? ??? ??? ??? ?.setFirstResult(firstResult).setMaxResults(maxResults);
?? ??? ??? ??? ?return query.list();
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?/**
?? ? * 删除一个对象
?? ? *
?? ? * @param entity
?? ? * @throws DataAccessException
?? ? */
?? ?public void delete(final Object entity) throws DataAccessException {
?? ??? ?execute(new HibernateCallback() {
?? ??? ??? ?public Object doInHibernate(Session session)
?? ??? ??? ??? ??? ?throws HibernateException, SQLException {
?? ??? ??? ??? ?session.delete(entity);
?? ??? ??? ??? ?return null;
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?/**
?? ? * 删除所有对象
?? ? *
?? ? * @param entities
?? ? * @throws DataAccessException
?? ? */
?? ?public void deleteAll(final Collection entities) throws DataAccessException {
?? ??? ?execute(new HibernateCallback() {
?? ??? ??? ?public Object doInHibernate(Session session)
?? ??? ??? ??? ??? ?throws HibernateException, SQLException {
?? ??? ??? ??? ?for (Iterator it = entities.iterator(); it.hasNext();) {
?? ??? ??? ??? ??? ?session.delete(it.next());
?? ??? ??? ??? ?}
?? ??? ??? ??? ?return null;
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?/**
?? ? * 保存对象
?? ? *
?? ? * @param obj
?? ? * @throws DataAccessException
?? ? */
?? ?public void save(final Object obj) throws DataAccessException {
?? ??? ?execute(new HibernateCallback() {
?? ??? ??? ?public Object doInHibernate(Session session)
?? ??? ??? ??? ??? ?throws HibernateException, SQLException {
?? ??? ??? ??? ?session.save(obj);
?? ??? ??? ??? ?return null;
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?/**
?? ? * 修改对象
?? ? *
?? ? * @param obj
?? ? * @throws DataAccessException
?? ? */
?? ?public void update(final Object obj) throws DataAccessException {
?? ??? ?execute(new HibernateCallback() {
?? ??? ??? ?public Object doInHibernate(Session session)
?? ??? ??? ??? ??? ?throws HibernateException, SQLException {
?? ??? ??? ??? ?session.update(obj);
?? ??? ??? ??? ?return null;
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?/**
?? ? * 保存或修改对象
?? ? *
?? ? * @param obj
?? ? * @throws DataAccessException
?? ? */
?? ?public void saveOrUpdate(final Object obj) throws DataAccessException {
?? ??? ?execute(new HibernateCallback() {
?? ??? ??? ?public Object doInHibernate(Session session)
?? ??? ??? ??? ??? ?throws HibernateException, SQLException {
?? ??? ??? ??? ?session.saveOrUpdate(obj);
?? ??? ??? ??? ?return null;
?? ??? ??? ?}
?? ??? ?});
?? ?}
}
HibernateCallback :
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
public interface HibernateCallback {
??? public Object doInHibernate(Session session) throws HibernateException,SQLException;
}
BaseHibernateDAO :
import infoair.fims.common.HibernateSessionFactory;
import infoair.fims.common.IBaseHibernateDAO;
import org.hibernate.Session;
public class BaseHibernateDAO implements IBaseHibernateDAO {
??? public Session getSession() {
??? ??? return HibernateSessionFactory.getSession();
??? }
??? public HibernateTemplate getTemplate() {
??? ??? return new HibernateTemplate();
??? }
}
//一个简单的操作类。对
FimnextdayschedDAO :
import infoair.fims.basic.Fimcurdailysched;
import infoair.fims.basic.Fimnextdaysched;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
public class FimnextdayschedDAO extends BaseHibernateDAO {
??? HibernateTemplate template = getTemplate();
??? /**
??? ?* 返回所有结果
??? ?*
??? ?* @return
??? ?* @throws DataAccessException
??? ?*/
??? public List findAll() throws DataAccessException {
??? ??? return template.findAll(Fimnextdaysched.class);
??? }
??? public void refreshFimnextdaysched() throws DataAccessException {
??? ??? template.execute(new HibernateCallback() {
??? ??? ??? public Object doInHibernate(Session session)
??? ??? ??? ??? ??? throws HibernateException, SQLException {
??? ??? ??? ??? session.createQuery("delete from Fimnextdaysched")
??? ??? ??? ??? ??? ??? .executeUpdate();
??? ??? ??? ??? session.save(new Fimnextdaysched());
??? ??? ??? ??? return null;
??? ??? ??? }
??? ??? });
??? }
??? public static void main(String[] args) {
??? ??? HibernateTemplate template = new HibernateTemplate();
??? ??? try {
??? ??? ??? List list = template.findAll(Fimnextdaysched.class);
??? ??? ??? List list2 = template.findAll("from Fimnextdaysched");
??? ??? ??? List list3 = template.findAll(Fimcurdailysched.class);
??? ??? ??? System.out.println(list);
??? ??? ??? System.out.println(list2);
??? ??? ??? System.out.println(list3);
??? ??? } catch (DataAccessException e) {
??? ??? ??? // TODO Auto-generated catch block
??? ??? ??? e.printStackTrace();
??? ??? }
??? }
}