public void saveEvent(Event event) { Event event1 = getEvent(event); Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { if (event1 == null) { Transaction transaction = session.beginTransaction(); try { session.save(event); transaction.commit(); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } } else { Transaction transaction = session.beginTransaction(); try { session.update(event); transaction.commit(); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } } } finally { if (session.isOpen()) { session.flush(); session.disconnect(); session.close(); } } }
public boolean createUser(User user) { boolean result = false; if (user.getEmployeeId() != null) { User userById = getUserByEmployeeId(user.getEmployeeId()); if (userById != null) { MessageUtils.createMessage("Brugeren eksisterer allerede."); return false; } } Session session = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction = session.beginTransaction(); try { session.saveOrUpdate(user); result = true; session.flush(); transaction.commit(); } catch (StaleObjectStateException sose) { System.out.println("StaleObjectException"); transaction.rollback(); } catch (HibernateException e) { e.printStackTrace(); transaction.rollback(); result = false; } finally { if (session.isOpen()) { session.close(); } } return result; }
public User getUser(String username) { User result = null; Session session; Object user; session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = null; user = null; try { transaction = session.beginTransaction(); Query query = session.createQuery("from User as user where user.userName='******'"); user = query.uniqueResult(); transaction.commit(); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } if (user != null) { result = (User) user; } } finally { if (session.isOpen()) { if (session.isOpen()) { session.disconnect(); session.close(); } } } return result; }
public List<Team> getTeams() { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); List result = null; try { Transaction transaction = session.beginTransaction(); result = session.createQuery("from Team").list(); session.flush(); transaction.commit(); } catch (HibernateException e) { e.printStackTrace(); } finally { if (session.isOpen()) { session.close(); } } List<Team> teams = new ArrayList<Team>(); for (Object o : result) { if (o instanceof Team) { teams.add((Team) o); } } return teams; }
public void saveObject(Object entity) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); session.saveOrUpdate(entity); transaction.commit(); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } }
public boolean saveUser(User user) { boolean result = false; Session session = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction = session.beginTransaction(); try { session.update(user); result = true; session.flush(); transaction.commit(); } catch (StaleObjectStateException sose) { System.out.println("StaleObjectException"); transaction.rollback(); } catch (HibernateException e) { e.printStackTrace(); transaction.rollback(); result = false; } finally { if (session.isOpen()) { session.close(); } } return result; }