⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basehibernatedao.java

📁 本系统是一个网上拍卖系统
💻 JAVA
字号:
package y2ssh.xzh.dao.hibimpl;

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

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

public abstract class BaseHibernateDAO {
	
	protected Session getSession() {
    	return HibernateSessionFactory.getSession();  
    }
  
	protected void add(Object item){
		try {
        	Transaction tx = getSession().beginTransaction();
        	getSession().save(item);
        	tx.commit();
        } catch (RuntimeException re) {
            throw re;
        }
	}
	
	protected Object get(Serializable id,Class clazz){
		try { 
			Object item = getSession()
            	.get(clazz, id);
			return item;
        } catch (RuntimeException re) {
            throw re;
        }
	}
	
	protected void del(Serializable id,Class clazz){
		try {
        	Transaction tx = getSession().beginTransaction();
        	getSession().delete(this.get(id, clazz));
        	tx.commit();
        } catch (RuntimeException re) {
            throw re;
        }		
	}
	
	protected void update(Object item){
		try {
        	Transaction tx = getSession().beginTransaction();
        	getSession().update(item);
        	tx.commit();
        } catch (RuntimeException re) {
            throw re;
        }
	}
	
	protected List search(Object condition,Class clazz){
		try {
            List results = getSession()
                    .createCriteria(clazz)
                    .add(Example.create(condition))
            .list();
            return results;
        } catch (RuntimeException re) {
            throw re;
        }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -