笑看风云

一切从头开始
posts - 28, comments - 1, trackbacks - 0, articles - 2

通用hibernateDAO

Posted on 2008-07-03 10:33 笑看风云 阅读(887) 评论(0)  编辑  收藏 所属分类: hibernate

package com.addressdemo.hibernate.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.addressdemo.HibernateSessionFactory;

public class BaseDAO {
   /** *//**
     * 添加实体
     * @param obj,要添加的实体对象
     * @throws Exception
     */
    public void add(Object obj) throws Exception{
        Session session = null;
        try {
            session = HibernateSessionFactory.getSession();
            session.save(obj);
            session.beginTransaction().commit();
            if(session!=null){
                  session.close();
            }
        } catch (RuntimeException e) {
            session.beginTransaction().rollback();
            if(session!=null){
                  session.close();
            }
            throw e;
        }
    }
   
    /** *//**
     * 删除实体
     * @param obj,要删除的实体
     * @throws Exception
     */
    public void delete(Object obj) throws Exception{
        Session session = null;
        try {
            //取得session对象
            session =HibernateSessionFactory.getSession();
            //删除实体
            session.delete(obj);
            //提交事务
            session.beginTransaction().commit();
            if(session!=null){
                  session.close();
            }
        } catch (Exception e) {
            session.beginTransaction().rollback();//事务回滚
            if(session!=null){
                  session.close();
            }
            throw e;
        }
    }

    /** *//**
     * 更新实体
     * @param obj,要更新的实体
     * @throws Exception
     */
    public void update(Object obj) throws Exception{
        Session session=null;
        try {
            //取得session对象
            session=HibernateSessionFactory.getSession();
            //删除实体
            session.update(obj);
            //提交事务
            session.beginTransaction().commit();
            if(session!=null){
                  session.close();
            }
        } catch (Exception e) {
            session.beginTransaction().rollback();//事务回滚
            if(session!=null){
              session.close();
            }
            throw e;
        }
    }
   
    /** *//**
     * 根据指定的hql进行查询,并返回查询结果
     * @param hql,hql语句
     * @return 查询结果
     * @throws Exception
     */
    public List<?> findByHQL(String hql) throws Exception{
        try {
            Query queryObject =HibernateSessionFactory.getSession().createQuery(hql);
            return queryObject.list();
        } catch (Exception e) {
            throw e;
        }
    }
    /** *//**
     * 根据指定的实体类型和主键的值,查找实体对象
     * @param cls,实体的类
     * @param key,主键的值
     * @return,查找的实体对象
     * @throws Exception
     */
    public Object findById(String cls,Serializable key)
        throws Exception
    {
        try {
            Object instance = (Object) HibernateSessionFactory.getSession().get(cls, key);
            return instance;
        } catch (Exception e) {
            throw e;
        }
       
    }

}


只有注册用户登录后才能发表评论。


网站导航: