basehibernatedao.java

来自「本系统是一个网上拍卖系统」· Java 代码 · 共 68 行

JAVA
68
字号
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 + =
减小字号Ctrl + -
显示快捷键?