hibernate 封装 2008-11-12 17:21 (分类:默认分类)
字號: 大 大 ? 中 中 ? 小 小
hibernate封裝
有幸和一個技術比較厲害的同事做項目,本來快要失敗的項目被死死的整活了,以下是他對hibernate的封裝,可以放在線程里使用的哦(以前不知道呢,-_-!,hibernate的功能真大啊 )
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[i]);?
???}?
???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[i],parameter[i]);
???}
???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[i];
?????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());
??}
?}
}
轉自我的校內:
http://blog.renren.com/blog/223579197/336228947?frommyblog
轉載于:https://www.cnblogs.com/yusha7/archive/2011/03/17/1987539.html
總結
以上是生活随笔為你收集整理的hibernate 封装 2008-11-12 17:21 (分类:默认分类)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET Form Authent
- 下一篇: JavaEE实战班第十七天