private static SessionFactory seesionFactory = null; private static final SessionFactory makeSessionFactory() { try { if (sessionFactory==null) seesionFactory = new Configuration().configure().buildSessionFactory(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); } return seesionFactory; }
Session session=null; try { session =HibernateUtil.getSessionFactory().openSession(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { session.close(); }
Session session=null; Transaction tx=null; try { session =HibernateUtil.getSessionFactory().openSession(); tx=session.beginTransaction(); } catch (HibernateException e) { e.printStackTrace(); }finally { session.close(); }
} catch (HibernateException e) { if (tx != null) { try { tx.rollback(); } catch(Exception re) { System.err.println("Error when trying to rollback transaction:"); // use logging framework here re.printStackTrace(); } } System.err.println("Original error when executing query:"); // // use logging framework here e.printStackTrace(); }
public Integer search(HashMap<String, Object> params) { Session sesion = sessionFactory.openSession(); Integer id = null; try{ List lista = sesion.createCriteria(User.class).add( Restrictions.eq("name",(String)params.get("name"))).list(); if(lista.size() == 1){id = ((User)lista.get(0)).getId();} }catch(HibernateException he){ he.printStackTrace(); }finally{sesion.close();} return id; }
public List<Car> findAll() { List<Car> carsList = new ArrayList<Car>(); try { query = session.createQuery("from Car"); carsList = query.list(); for (Iterator iterator = carsList.iterator(); iterator.hasNext(); ) { Car car = (Car) iterator.next(); } } catch (HibernateException he) { he.printStackTrace(); } finally { } return carsList; }
Session session = HibernateUtil.getSession(); try { String hql = "FROM TreeNode tn JOIN tn.nodeAsset WHERE tn.id=5"; Query query = session.createQuery(hql); List result = query.list(); System.out.println("done"); } catch (HibernateException e) { e.printStackTrace(); throw new Exception("Query failed", e); } finally { session.flush(); session.close(); }
public String getCustomerEmail(){ Session session = HibernateSessionFactory.getSession(); //Session session = sessionFactory.openSession(); String maxOrderNoDay = ""; try{ maxOrderNoDay = (String) session.createQuery("select cus.mail from Customer cus join cus.mainOrder man WHERE man.id = 1").uniqueResult(); // System.out.println(maxOrderNoDay); }catch(HibernateException e){ e.printStackTrace(); }finally{ session.close(); return maxOrderNoDay; } }
try { System.out.println("Begin transaction"); session.beginTransaction(); System.out.println("Save mortgage"); session.save(mortgage); System.out.println("Commit transaction"); session.getTransaction().commit(); System.out.println("Close session"); session.close(); System.out.println("Close SessionFactory"); HibernateUtil.getSessionFactory().close(); } catch (HibernateException he) { he.printStackTrace(); }
private Session getSession(SessionFactory sessionFactory){ Session session = null; try{ session = sessionFactory.getCurrentSession(); }catch(HibernateException hex){ hex.printStackTrace(); } if(session == null && !session.isClosed()){ session = sessionFactory.openSession(); } }
public void shutdown() { try { sessionFactory.close(); sessionTL.remove(); interceptorTL.remove(); } catch (HibernateException e) { e.printStackTrace(System.out); } sessionFactory = null; }
public static void setConnectionProperties() { URL configFileURL = null; try { configFileURL = URL_OF_YOUR_CFG_XML_FILE; configuration = (new Configuration()).configure(configFileURL); sessionFactory = configuration.buildSessionFactory(); } catch (HibernateException e) { e.printStackTrace(); System.out.println("Error while initializing hibernate: " + e.getMessage()); } }
String[] splitQueryStr = queryString.split(";"); for (String queryStr : splitQueryStr) { try { getSession().beginTransaction(); getSession().createSQLQuery(queryStr).executeUpdate(); getSession().getTransaction().commit(); } catch (HibernateException he) { getSession().getTransaction().rollback(); he.printStackTrace(); throw new HibernateException("Hibernate Error"); } catch (Exception e) { getSession().getTransaction().rollback(); throw new Exception(); } }
public void deleteEmployee (Integer EmployeeID){ Session session = factory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Employee employee = (Employee) session.get(Employee.class, EmployeeID); session.delete(employee); tx.commit(); } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); } finally { session.close(); } }
public void addGrade(Grade grade) { Session session = sessionFactory.getCurrentSession() Transaction tx = null; try{ tx = session.beginTransaction(); session.save(grade); tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } }
public TestType getTestTypeById(int idTestType) { Session session = sf.openSession(); // sf as a sessionFactory instance Transaction tx = null; TestType testType = null; try { tx = session.beginTransaction(); testType = (TestType) session.get(TestType.class, idTestType); tx.commit(); } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); } finally { session.close(); } return testType; }
Session s = HibernateUtil.getSessionFactory().openSession(); Transaction tx = null; try { tx = s.beginTransaction(); // here get object List<Employee> list = s.createCriteria(Employee.class).list(); tx.commit(); } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } Logger.getLogger("con").info("Exception: " + ex.getMessage()); ex.printStackTrace(System.err); } finally { s.close(); }
public void save(Object entity) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); session.save(entity); transaction.commit(); } catch (HibernateException e) { System.out.println("Exception:"+e.getMessage()); transaction.rollback(); e.printStackTrace(); } finally { session.close(); } }
public User updateUser(User user) { try { User result = session.get(User.class, user.getId()); if (result == null) { throw new FilamentNoSuchRecordException(new CoreError(304, "User does not exist")); } result.setName(user.getName()); // update some properties session.update(result); // you should update 'result', not 'user' return result; } catch (HibernateException e) { e.printStackTrace(); throw new FilamentDataConnectivityException(new CoreError(305,"Connectivity issue. Please see System Administrator")); } }
public void addRolesToUser(Role roles,UserDao user) { try{ session.beginTransaction(); roles=new Role(); user=new UserDao(); user.getRoles().add(roles); session.save(roles); session.save(user); session.getTransaction().commit(); }catch(HibernateException e){ session.getTransaction().rollback(); e.printStackTrace(); } }