日期:2014-05-19 浏览次数:20778 次
public static void save(User user) { Session session = null; Transaction transaction = null; try { session = HibernateUtils.getSession(); transaction = session.beginTransaction(); session.save(user); transaction.commit(); } catch (Exception e) { e.printStackTrace(); if (transaction != null) { transaction.rollback(); } } finally { HibernateUtils.closeSession(session); } } public static User get(String id) { if (id == null || id.length() == 0) { return null; } Session session = null; Transaction transaction = null; User user = null; try { session = HibernateUtils.getSession(); transaction = session.beginTransaction(); user = (User) session.get(User.class, id); transaction.commit(); } catch (Exception e) { e.printStackTrace(); if (transaction != null) { transaction.rollback(); } } finally { HibernateUtils.closeSession(session); } return user; }