📄 basedaohibernate.java
字号:
/**
*
*/
package cn.bway.common.dao;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import cn.bway.common.BwayHibernateException;
import cn.bway.common.vo.PageListVO;
import cn.bway.common.vo.QueryVO;
/**
* @author Kson
*
*/
public class BaseDAOHibernate implements DAO {
protected final Logger log = LogManager.getLogger(getClass());
protected static Session session=null;
/**
* ��sessionFactory�õ���ǰ�̵߳�session
* @return
*/
public static Session currentSession()
{
return HibernateSessionFactory.currentSession();
}
public Session currentSession2()
{
return HibernateSessionFactory.currentSession();
}
/**
* �ر�session
*/
public static void closeSession()
{
HibernateSessionFactory.closeSession();
}
/**
* �������
* @see org.appfuse.dao.DAO#saveObject(java.lang.Object)
*/
public void saveObject(Object o)throws BwayHibernateException
{
Transaction tx=null;
try{
session=currentSession();
tx=session.beginTransaction();
session.save(o);
tx.commit();
}catch(HibernateException he){
tx.rollback();
log.debug(o.getClass().getName() + "saveObject(Object o)"+he);
throw new BwayHibernateException(o.getClass().getName() +he);
}finally{
closeSession();
}
}
/**
* ��������ҷ��ض����ID
*/
public Serializable saveObject_ReturnID(Object o) throws BwayHibernateException
{
Serializable id=null;
Transaction tx=null;
try{
session=currentSession();
tx=session.beginTransaction();
id=session.save(o);
tx.commit();
}catch(HibernateException he){
tx.rollback();
he.printStackTrace();
log.debug(this.getClass().getName() + "saveObject_ReturnID(Object o)"+he);
throw new BwayHibernateException(he.getMessage());
}catch(Exception ex){
ex.printStackTrace();
}finally{
closeSession();
}
return id;
}
/**
* ͨ��IDȡ�ö���
* @throws BwayHibernateException
* @see org.appfuse.dao.DAO#getObject(java.lang.Class, java.io.Serializable)
*/
public Object getObject(Class clazz, Serializable id)
{
Object o =null;
if(id==null){
log.info(this.getClass().getName()+".getObject() id is null!");
return null;
}
try{
session=currentSession();
o=session.get(clazz, id);
}catch(HibernateException he){
he.printStackTrace();
log.error((this.getClass().getName()+".getObject():"+he));
}finally{
closeSession();
}
if (o == null) {
log.debug(clazz.getName()+id+"is null!");
}
return o;
}
/**
* ��ҳ��ѯ
*/
public Object getObjects(Class clazz,QueryVO qvo) throws BwayHibernateException
{
PageListVO rvo=new PageListVO();
rvo.setRealPage(qvo.getCurPage());
List list=null;
try{
session=currentSession();
Criteria criteria=session.createCriteria(clazz);
rvo.setTotalItems(criteria.list().size());
if(qvo!=null){
criteria.setFirstResult(qvo.getFirstItem());
criteria.setMaxResults(qvo.getPageSize());
}
list=criteria.list();
}catch(HibernateException he)
{
log.error(this.getClass().getName()+".getObjects(Class clazz,QueryVO qvo)"+he);
throw new BwayHibernateException(he.getMessage());
}finally{
closeSession();
}
if(list!=null){
rvo.setretVO(list);
}
return rvo;
}
/**
* ����ҳ��ѯ
*/
public Object getObjects(Class clazz) throws BwayHibernateException
{
List list=null;
try{
session=currentSession();
Criteria criteria=session.createCriteria(clazz);
list=criteria.list();
}catch(HibernateException he){
log.error(this.getClass().getName()+".getObjects(Class clazz)"+he);
throw new BwayHibernateException(he.getMessage());
}finally{
closeSession();
}
return list;
}
/**
* ͨ��ID�Ƴ����
*/
public void removeObject(Class clazz, Serializable id) throws BwayHibernateException
{
Transaction tx=null;
try {
session=currentSession();
tx=session.beginTransaction();
Object object=session.get(clazz, id);
session.delete(object);
session.flush();
tx.commit();
} catch (HibernateException e) {
tx.rollback();
log.error(this.getClass().getName()+".removeObject(Class clazz, Serializable id)"+e);
throw new BwayHibernateException(e.getMessage());
}finally{
closeSession();
}
}
/**
* ���¶���
*/
public void updateObject(Object obj) throws BwayHibernateException
{
Transaction tx=null;
try {
session=currentSession();
tx=session.beginTransaction();
session.update(obj);
tx.commit();
} catch (HibernateException e)
{
tx.rollback();
e.printStackTrace();
log.error(this.getClass().getName()+".updateObject(Object obj)"+e);
throw new BwayHibernateException(e.getMessage());
}finally{
closeSession();
}
}
/**
* �ر�statement ��Resultset����
* @param stat
* @param rs
*/
public void close(Statement stat,ResultSet rs)
{
try {
if(rs!=null)
rs.close();
if(stat!=null)
stat.close();
} catch (SQLException e) {
log.error(this.getClass().getName()+".close(Connection conn,Statement stat,ResultSet rs)"+e);
}
}
/**
* �ر�statement ��Resultset����
* @param stat
* @param rs
*/
public void close(Statement stat,ResultSet rs,Connection conn)
{
try {
if(rs!=null)
rs.close();
if(stat!=null)
stat.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
log.error(this.getClass().getName()+".close(Statement stat,ResultSet rs,Connection conn)"+e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -