您的位置:首页 > 理论基础 > 计算机网络

Hibernate DAO封装【来自网络】

2011-03-25 10:53 375 查看
package com.eclink.editp.dao;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.cn.hibernate.SessionFactory;
/**
* @author xiongying
* @Date 2006-02-25
* @declare 关于HIBERNATE的相关方法的封装
*/
public class HibernateDao {
private static final Logger logger = Logger.getLogger(HibernateDao.class);
List list = new ArrayList();
/**
* @param entityName
* @param id
* @return
*/
public Object get(Class entityName, Serializable id) {
logger.info("get(Class entityName, int id) - start");
Session session = null;
Transaction tx = null;
try {
session = SessionFactory.currentSession();//最好是工具生成的SessionFactory,自己写比较麻烦,想我这种菜鸟还会出错
tx = session.beginTransaction();
Object object = session.get(entityName, id);
tx.commit();
SessionFactory.closeSession();
logger.info("get(Class entityName, int id) - end");
return object;
} catch (HibernateException e) {
logger.error("get(Class entityName, int id) throw exception = ", e);
try {
if (tx != null) tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = " + ex.toString());
}
throw new RuntimeException("get(Class entityName, int id) throw exception = " + e.toString());
} finally {
try {
if (session != null) SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory.closeSession() throw exception = " + e.toString());
}
}
}
/**
* @param object
* @return
*/
public boolean delete(Object object) {
Session session = null;
Transaction tx = null;
logger.info("delete(Object object) - start");
try {
session = SessionFactory.currentSession();
tx = session.beginTransaction();
session.delete(object);
tx.commit();
SessionFactory.closeSession();
logger.info("delete(Object object) - end");
return true;
} catch (HibernateException e) {
logger.error("delete(Object object) throw exception = ", e);
try {
if (tx != null) tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = " + ex.toString());
}
throw new RuntimeException("delete(Object object) throw exception = " + e.toString());
} finally {
try {
if (session != null) SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory.closeSession() throw exception = " + e.toString());
}
}
}
/**
* @param object
* @return
* @throws HibernateException
*/
public boolean save(Object object) {
Session session = null;
Transaction tx = null;
logger.info("save(Object object) - start");
try {
session = SessionFactory.currentSession();
tx = session.beginTransaction();
session.save(object);
tx.commit();
SessionFactory.closeSession();
logger.info("save(Object object) - end");
return true;
} catch (HibernateException e) {
logger.error("save(Object object) throw exception = ", e);
try {
if (tx != null) tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = " + ex.toString());
}
throw new RuntimeException("save(Object object) throw exception = " + e.toString());
} finally {
try {
if (session != null) SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory.closeSession() throw exception = " + e.toString());
}
}
}
/**
* @param object
* @return
* @throws HibernateException
*/
public boolean update(Object object) {
Session session = null;
Transaction tx = null;
logger.info("update(Object object) - start");
try {
session = SessionFactory.currentSession();
tx = session.beginTransaction();
session.update(object);
tx.commit();
SessionFactory.closeSession();
logger.info("update(Object object) - end");
return true;
} catch (HibernateException e) {
logger.error("update(Object object) throw exception = ", e);
try {
if (tx != null) tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = " + ex.toString());
}
throw new RuntimeException("update(Object object) throw exception = " + e.toString());
} finally {
try {
if (session != null) SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory.closeSession() throw exception = " + e.toString());
}
}
}
/**
* @param object
* @return
* @throws HibernateException
*/
public boolean saveOrUpdate(Object object) {
System.out.println("=============== saveOrUpdate() start =================");
Session session = null;
Transaction tx = null;
logger.info("saveOrUpdate(Object) - start");
try {
session = SessionFactory.currentSession();
tx = session.beginTransaction();
session.saveOrUpdate(object);
tx.commit();
SessionFactory.closeSession();
logger.info("saveOrUpdate(Object) - end");
System.out.println("============== saveOrUpdate() end =================");
return true;
} catch (HibernateException e) {
logger.error("saveOrUpdate(Object object) throw exception = ", e);
try {
if (tx != null) tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = " + ex.toString());
}
throw new RuntimeException("saveOrUpdate(Object) throw exception = " + e.toString());
} finally {
try {
if (session != null) SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory.closeSession() throw exception = " + e.toString());
}
}
}
/**
* @param sql
* @param parameter
* @return
* @throws HibernateException
*/
public boolean update(String sql, Object[] parameter) {
Session session = null;
Transaction tx = null;
logger.info("update(String sql, Object[] parameter) - start");
try {
session = SessionFactory.currentSession();
tx = session.beginTransaction();
Query query = session.createQuery(sql);
setQueryParameterValues(query, parameter);
query.executeUpdate();
tx.commit();
SessionFactory.closeSession();
logger.info("update(String sql, Object[] parameter) - end");
return true;
} catch (HibernateException e) {
logger.error("update(String sql, Object[] parameter) throw exception = ", e);
try {
if (tx != null) tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = " + ex.toString());
}
throw new RuntimeException("update(String sql, Object[] parameter) throw exception = " + e.toString());
} finally {
try {
if (session != null) SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory.closeSession() throw exception = " + e.toString());
}
}
}
/**
* @param sql
* @return
*/
public List find(String sql) {
logger.info("find(String, String, int) - start");
Session session = null;
Transaction tx = null;
try {
session = SessionFactory.currentSession();
tx = session.beginTransaction();
Query query = session.createQuery(sql);
list = query.list();
tx.commit();
SessionFactory.closeSession();
logger.info("find(String, String, int) - end");
return list;
} catch (HibernateException e) {
logger.error("find(String, String, int) throw exception = ", e);
try {
if (tx != null) tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = " + ex.toString());
}
throw new RuntimeException("find(String, String, int) throw exception = " + e.toString());
} finally {
try {
if (session != null) SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory.closeSession() throw exception = " + e.toString());
}
}
}
/**
* 位置参数查询
* @param sql
* @param parameter
* @return
*/
public List find(String sql, Object[] parameter) {
Session session = null;
Transaction tx = null;
logger.info("find(String sql, Object[] object) - start");
try {
session = SessionFactory.currentSession();
tx = session.beginTransaction();
Query query = session.createQuery(sql);
setQueryParameterValues(query, parameter);
list = query.list();
tx.commit();
SessionFactory.closeSession();
logger.info("find(String sql, Object[] object) - end");
return list;
} catch (HibernateException e) {
logger.error("find(String sql, Object[] object) throw exception = ", e);
try {
if (tx != null) tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = " + ex.toString());
}
throw new RuntimeException("find(String sql, Object[] object) throw exception = " + e.toString());
} finally {
try {
if (session != null) SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory.closeSession() throw exception = " + e.toString());
}
}
}
/**
* 命名参数查询
* @param sql
* @param name
* @param parameter
* @return
*/
public List find(String sql, String[] name, Object[] parameter) {
Session session = null;
Transaction tx = null;
logger.info("find(String sql, String[] name, Object[] object) - start");
try {
session = SessionFactory.currentSession();
tx = session.beginTransaction();
Query query = session.createQuery(sql);
setQueryParameterValues(query, name, parameter);
list = query.list();
tx.commit();
SessionFactory.closeSession();
logger.info("find(String sql, String[] name, Object[] object) - end");
return list;
} catch (HibernateException e) {
logger.error("find(String sql, String[] name, Object[] object) throw exception = ", e);
try {
if (tx != null) tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = " + ex.toString());
}
throw new RuntimeException("find(String sql, String[] name, Object[] object) object) throw exception = " + e.toString());
} finally {
try {
if (session != null) SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory.closeSession() throw exception = " + e.toString());
}
}
}
/**
* @param sql
* @param firstSize
* @param pageSize
* @return
*/
public List findPage(String sql, int firstSize, int pageSize) {
Session session = null;
Transaction tx = null;
try {
logger.info("findPage(String sql, int firstSize, int pageSize) - start");
session = SessionFactory.currentSession();
tx = session.beginTransaction();
Query query = session.createQuery(sql);
query.setFirstResult(firstSize);
query.setMaxResults(pageSize);
list = query.list();
tx.commit();
SessionFactory.closeSession();
logger.info("findPage(String sql, int firstSize, int pageSize)) - end");
return list;
} catch (HibernateException e) {
logger.error("findPage(String sql, int firstSize, int pageSize) throw exception = ", e);
try {
if (tx != null) tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = " + ex.toString());
}
throw new RuntimeException("findPage(String sql, int firstSize, int pageSize) throw exception = " + e.toString());
} finally {
try {
if (session != null) SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory.closeSession() throw exception = " + e.toString());
}
}
}
/**
* 位置参数 分页查询
* @param sql
* @param parameter
* @param firstSize
* @param pageSize
* @return
*/
public List findPage(String sql, Object[] parameter, int firstSize, int pageSize) {
Session session = null;
Transaction tx = null;
logger.info("findPage(String sql, Object[] object, int firstSize, int maxSize) - start");
try {
session = SessionFactory.currentSession();
tx = session.beginTransaction();
Query query = session.createQuery(sql);
setQueryParameterValues(query, parameter);
query.setFirstResult(firstSize);
query.setMaxResults(pageSize);
list = query.list();
tx.commit();
SessionFactory.closeSession();
logger.info("findPage(String sql, Object[] object, int firstSize, int maxSize) - end");
return list;
} catch (HibernateException e) {
logger.error("findPage(String sql, Object[] object, int firstSize, int maxSize) throw exception = ", e);
try {
if (tx != null) tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = " + ex.toString());
}
throw new RuntimeException("findPage(String sql, Object[] object, int firstSize, int maxSize) throw exception = " + e.toString());
} finally {
try {
if (session != null) SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory.closeSession() throw exception = " + e.toString());
}
}
}
/**
* 命名参数 分页查询
* @param sql
* @param name
* @param parameter
* @param firstSize
* @param pageSize
* @return
*/
public List findPage(String sql, String[] name , Object[] parameter, int firstSize, int pageSize) {
Session session = null;
Transaction tx = null;
logger.info("findPage(String sql, String[] name , Object[] object, int firstSize, int maxSize) - start");
try {
session = SessionFactory.currentSession();
tx = session.beginTransaction();
Query query = session.createQuery(sql);
setQueryParameterValues(query, name ,parameter);
query.setFirstResult(firstSize);
query.setMaxResults(pageSize);
list = query.list();
tx.commit();
SessionFactory.closeSession();
logger.info("findPage(String sql, String[] name , Object[] object, int firstSize, int maxSize) - end");
return list;
} catch (HibernateException e) {
logger.error("findPage(String sql, String[] name , Object[] object, int firstSize, int maxSize) throw exception = ", e);
try {
if (tx != null) tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = " + ex.toString());
}
throw new RuntimeException("findPage(String sql, String[] name , Object[] object, int firstSize, int maxSize) throw exception = " + e.toString());
} finally {
try {
if (session != null) SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory.closeSession() throw exception = " + e.toString());
}
}
}
/**
* 滚动查询
* @param sql
* @param parameter
* @return
*/
public List findScrollable(String sql, Object[] parameter) {
Session session = null;
Transaction tx = null;
logger.info("findScrollable(String sql, Object[] object) - start");
ScrollableResults srs = null;
List list = new ArrayList();
int j;
try {
session = SessionFactory.currentSession();
tx = session.beginTransaction();
Query query = session.createQuery(sql);
System.out.println("query before");
setQueryParameterValues(query, parameter);
srs = query.scroll();// ===有滚动;
while (srs.next()) {
Vector vec = new Vector();
j = 0;
// ===将查询的字段值循环放入到集合中;
while (true) {
try {
vec.add(srs.get(j)); // ===当没有字段值时,此处自动会抛出一个异常终止;
j++;
} catch (Exception e) {
logger.error("findScrollable(String sql, Object[] object)", e);
break;
}
}
list.add(vec);
}
tx.commit();
SessionFactory.closeSession();
logger.info("findScrollable(String sql, Object[] object) - end");
return list;
} catch (HibernateException e) {
logger.error("findScrollable(String sql, Object[] object) throw exception = ", e);
try {
if (tx != null) tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = " + ex.toString());
}
throw new RuntimeException("findScrollable(String sql, Object[] object) throw exception = " + e.toString());
} finally {
try {
if (session != null) SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory.closeSession() throw exception = " + e.toString());
}
}
}
/**
* 位置参数设置
* @param query
* @param parameter
*/
public void setQueryParameterValues(Query query, Object[] parameter){
logger.info("setQueryParameterValues(Query query, Object[] object) - start");
try {
if(parameter!=null){
for(int i=0;i<parameter.length;i++) query.setParameter(i,parameter); }
logger.info("setQueryParameterValues(Query query, Object[] object) - end");
} catch (HibernateException e) {
logger.error("setQueryParameterValues(Query query, Object[] object) throw exception = ", e);
throw new RuntimeException("setQueryParameterValues(Query query, Object[] object) throw exception = " + e.toString());
}
}
/**
* 命名参数设置
* @param query
* @param name
* @param parameter
*/
public void setQueryParameterValues(Query query, String[] name , Object[] parameter){
logger.info("setQueryParameterValues(Query query, String[] str , Object[] object) - start");
try {
if (name.length != parameter.length) {
throw new IllegalArgumentException("setQueryParameterValues(Query query, String[] name , Object[] parameter) = Length of paramNames array must match length of values array");
}
if(name!=null && parameter!=null){
for(int i=0;i<name.length;i++) query.setParameter(name,parameter); } logger.info("setQueryParameterValues(Query query, String[] str , Object[] object) - end");
} catch (HibernateException e) {
logger.error("setQueryParameterValues(Query query, String[] str , Object[] object) throw exception = ", e);
throw new RuntimeException("setQueryParameterValues(Query query, String[] str , Object[] object) throw exception = " + e.toString());
}
}
/**
* @param parameter
* @param query
* @throws HibernateException
*/
public void setQueryParameter(Query query, Object[] parameter){
logger.info("setQueryParameter(Query query, Object[] object) - start");
Object pValue = null;
try {
if (parameter != null) {
for (int i = 0; i < parameter.length; i++) {
pValue = parameter; if (pValue instanceof String) {
query.setString(i, (String) pValue);
} else if (pValue instanceof Integer) {
query.setInteger(i, ((Integer) pValue).intValue());
} else if (pValue instanceof Boolean) {
query.setBoolean(i, ((Boolean) pValue).booleanValue());
} else if (pValue instanceof Short) {
query.setShort(i, ((Short) pValue).shortValue());
} else if (pValue instanceof Long) {
query.setLong(i, ((Long) pValue).longValue());
} else if (pValue instanceof Float) {
query.setFloat(i, ((Float) pValue).floatValue());
} else if (pValue instanceof Double) {
query.setDouble(i, ((Double) pValue).doubleValue());
} else if (pValue instanceof BigDecimal) {
query.setBigDecimal(i, (BigDecimal) pValue);
} else if (pValue instanceof Byte) {
query.setByte(i, ((Byte) pValue).byteValue());
} else if (pValue instanceof java.sql.Date) {
query.setDate(i, java.sql.Date.valueOf(pValue.toString()));
} else if (pValue instanceof java.sql.Time) {
query.setTime(i, java.sql.Time.valueOf(pValue.toString()));
} else if (pValue instanceof java.sql.Timestamp) {
query.setTimestamp(i, java.sql.Timestamp.valueOf(pValue.toString()));
} else if (pValue instanceof java.util.Date) {
query.setDate(i, java.sql.Date.valueOf(pValue.toString()));
} else {
// query.setObject(i, pValue);
}
}
}
logger.info("setQueryParameter(Query query, Object[] object) - end");
} catch (HibernateException e) {
logger.error("setQueryParameter(Query query, Object[] object)", e);
throw new RuntimeException("setQueryParameter(Query query, Object[] object) throw exception = " + e.toString());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: