灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:4159回复:0

hibernate增删改查的标准范例

楼主#
更多 发布于:2013-01-10 15:33
一个套用hibernate框架编写的增删改查小范例,此处分享一下,经过多次修改,从代码规范和后期维护,以及简洁程度上说:算是很标准的书写格式;
 
[java]  
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();  
        }  
    }  
   }  

喜欢0 评分0
游客

返回顶部