日期:2014-05-20 浏览次数:20822 次
public class DbOperate { public boolean save(Object obj) throws HibernateException { boolean result = false; Session session = SessionFactory.currentSession(); if (obj != null) { Transaction tx = null; try { tx = session.beginTransaction(); session.save(obj); tx.commit(); result = true; } catch (HibernateException e) { if (tx != null) { tx.rollback(); result = false; } } finally { session.close(); return result; } } return result; } public void update(Object obj) throws HibernateException { Session session = SessionFactory.currentSession(); if (obj != null) { Transaction tx = null; try { tx = session.beginTransaction(); ////System.out.println("aaaa"); session.update(obj); ////System.out.println("bbbb"); tx.commit(); ////System.out.println("cccc"); } catch (HibernateException e) { if (tx != null) tx.rollback(); throw e; } } session.close(); } public void delete(Object obj) throws HibernateException { Session session = SessionFactory.currentSession(); if (obj != null) { Transaction tx = null; try { tx = session.beginTransaction(); session.delete(obj); tx.commit(); } catch (HibernateException e) { if (tx != null) tx.rollback(); throw e; } } session.close(); } public void saveOrUpdate(Object obj) throws HibernateException { Session session = SessionFactory.currentSession(); if (obj != null) { Transaction tx = null; try { tx = session.beginTransaction(); session.saveOrUpdate(obj); tx.commit(); } catch (HibernateException e) { if (tx != null) tx.rollback(); throw e; } } session.close(); } public List find(String sql) throws HibernateException { Session session = SessionFactory.currentSession(); List list = null; Transaction tx = null; try { tx = session.beginTransaction(); Query query = session.createQuery(sql); list = query.list(); tx.commit(); } catch (HibernateException e) { if (tx != null) tx.rollback(); throw e; } session.close(); return list; } }