hibernate增删改查的标准范例

    一个套用hibernate框架编写的增删改查小范例,此处分享一下,经过多次修改,从代码规范和后期维护,以及简洁程度上说:算是很标准的书写格式;

package www.csdn.net.bookhome.daoimpl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import www.csdn.net.bookhome.dao.AdminDao;
import www.csdn.net.bookhome.dao.BaseHibernateDao;
import www.csdn.net.bookhome.domain.Admin;
import www.csdn.net.bookhome.utils.HibernateSessionFactory;

public class AdminDaoImpl extends BaseHibernateDao implements AdminDao {

	public void deleteObject(Admin entity) {
		Transaction tx = null;
		try {
			Session session = getSession();
			tx = session.beginTransaction();
			session.delete(entity);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw new RuntimeException("删除所有错误"+e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	public void deleteObjectById(Integer id) {
		Transaction tx = null;
		try {
			Session session = getSession();
			tx = session.beginTransaction();
			session.save(id);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw new RuntimeException("根据id错误"+e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	public List getAllObjects(Class entityClass) {
		try {
			return getSession().createQuery("from Admin").list();
		} catch (Exception e) {
			throw new RuntimeException("查找错误"+e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	public Admin getObjectById(Class className, Integer id) {
		try {
			return (Admin) getSession().get(className, id);
		} catch (Exception e) {
			throw new RuntimeException("根据id查找错误"+e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	public List getObjects(Class clazz, int from, int size, String orderName) {
		try {
			return getSession().createQuery("from Admin").setFirstResult((from-1)*size).setMaxResults(size).list();
		} catch (Exception e) {
			throw new RuntimeException("分页查询错误"+e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	public Admin loadObjectById(Class className, Integer id) {
		try {
			return (Admin) getSession().load(className, id);
		} catch (Exception e) {
			throw new RuntimeException("load查询错误"+e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	public void saveObject(Admin entity) {
		Transaction tx = null;
		try {
			Session session = getSession();
			tx = session.beginTransaction();
			session.save(entity);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw new RuntimeException("保存错误"+e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	public void updateObject(Admin entity) {
		Transaction tx = null;
		try {
			Session session = getSession();
			tx = session.beginTransaction();
			session.update(entity);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw new RuntimeException("更新错误"+e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	public Admin getAllObjects(String name) {
		
		try {			
			return (Admin) getSession().createQuery("from Admin a where a.adminName = :name")
			.setParameter("name", name).uniqueResult();
		} catch (Exception e) {
			throw new RuntimeException("根据用户查询有错误"+e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	public Admin login(Admin entity) {
		try {
			return (Admin) getSession().createQuery("from Admin a where a.adminName = :name and a.adminPassword = :pass ").setString("name",entity.getAdminName()).setString("pass", entity.getAdminPassword()).uniqueResult();
		} catch (Exception e) {
			throw new RuntimeException("登录错误"+e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

}


 

posted @ 2013-01-09 14:39  yangkai_keven  阅读(111)  评论(0编辑  收藏  举报