List of usage examples for org.hibernate.boot.registry StandardServiceRegistryBuilder StandardServiceRegistryBuilder
public StandardServiceRegistryBuilder()
From source file:Search.GetMovies.java
public static void listMovies(String movname) { Configuration configuration = new Configuration(); configuration.configure();/*from w ww .j a v a 2 s.com*/ serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); Session session = new Configuration().configure().buildSessionFactory(serviceRegistry).openSession(); /*double p1[]={0.1,0.2,0.3,0.4}; double p2[]={0.15,0.2,0.4,0.25};*/ Transaction tx = null; try { tx = session.beginTransaction(); System.out.println(movname); String qry = "select m.movid from GS_Movie m where m.name= :movname"; Query p1 = session.createQuery(qry); p1.setParameter("movname", movname); List p11 = p1.list(); try { //System.out.println(p11.iterator().next()); String movid = (String) p11.iterator().next(); //System.out.println(movid); Query qrya = session.createQuery("From GS_Movkld l where l.movid=:movid"); qrya.setParameter("movid", movid); List movies1 = qrya.list(); Iterator iterator1 = movies1.iterator(); GS_Movkld moviea = (GS_Movkld) iterator1.next(); ArrayList<Double> m1 = new ArrayList<Double>(); m1.add(moviea.getAction()); m1.add(moviea.getAdventure()); m1.add(moviea.getAnimation()); m1.add(moviea.getComedy()); m1.add(moviea.getCrime()); m1.add(moviea.getDrama()); //Query qry1=session.createQuery("From GS_Movkld"); // qry1.setParameter("movid", movid); Query qryb = session.createQuery("From GS_Movkld l where l.movid!=:movid"); qryb.setParameter("movid", movid); List movies = qryb.list(); int i = 0; HashMap<String, Double> map = new HashMap<String, Double>(); // ValueComparator bvc = new ValueComparator(map); // TreeMap<String,Double> sorted_map = new TreeMap<String,Double>(bvc); //System.out.println(movies); for (Iterator iterator = movies.iterator(); iterator.hasNext();) { GS_Movkld movie = (GS_Movkld) iterator.next(); ArrayList<Double> m2 = new ArrayList<>(); m2.add(movie.getAction()); m2.add(movie.getAdventure()); m2.add(movie.getAnimation()); m2.add(movie.getComedy()); m2.add(movie.getCrime()); m2.add(movie.getDrama()); double[] m11 = Doubles.toArray(m1); double[] m22 = Doubles.toArray(m2); //Double temp = KLDivergence.klDivergence(m11, m22); Double temp; if ((temp = KLDivergence.klDivergence(m11, m22)) <= 0.29999876) { /* System.out.println("Movie #" + i); System.out.println("Action: " + movie.getAction()); System.out.println("Comedy: " + movie.getComedy()); System.out.println("Drama: " + movie.getDrama()); i++;*/ map.put(movie.getMovid(), temp); } else { //System.out.println("Not recommended"); continue; } } ArrayList as = new ArrayList(map.entrySet()); Collections.sort(as, new Comparator() { public int compare(Object o1, Object o2) { Map.Entry e1 = (Map.Entry) o1; Map.Entry e2 = (Map.Entry) o2; Double first = (Double) e1.getValue(); Double second = (Double) e2.getValue(); return first.compareTo(second); } }); //List as1=as.subList(0, 5); Iterator j = as.iterator(); while (j.hasNext() && i < 5) { //System.out.println( (Map.Entry)j.next()); System.out.println(((Map.Entry) j.next()).getKey()); i++; } tx.commit(); } catch (NoSuchElementException nsee) { System.out.println("No Such Movie Found!!"); } // response.sendRedirect("localhost:8080/RecSysJava"); } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); } finally { session.close(); } }
From source file:Search.UpdateMovie.java
private void updateMov(String name, String year, String plot, String dir, String prod, String movid, String cast, String genre) { Configuration configuration = new Configuration(); configuration.configure();// w w w .j a v a2 s . c om serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); Session session = new Configuration().configure().buildSessionFactory(serviceRegistry).openSession(); Transaction tx = null; try { tx = session.beginTransaction(); String hql = "UPDATE GS_Movie m set m.year = :year, m.plot=:plot, m.cast=:cast, m.genre=:genre, m.director=:dir, m.producer=:prod " + "WHERE m.movid = :movid"; Query query = session.createQuery(hql); query.setParameter("year", year); query.setParameter("plot", plot); query.setParameter("cast", cast); query.setParameter("dir", dir); query.setParameter("prod", prod); query.setParameter("movid", movid); query.setParameter("genre", genre); int result = query.executeUpdate(); System.out.println("Rows affected: " + result); tx.commit(); } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); } finally { session.close(); } }
From source file:servlet.DeleteSR.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from ww w. ja v a2 s . c o m * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet DeleteSR</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet DeleteSR at " + request.getContextPath() + "</h1>"); // No try catch, I suppose the connexion will be okey... Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build()); Session session = sessionFactory.openSession(); Transaction trans = session.beginTransaction(); try { //String id = request.getParameter("sc_id").trim(); int sId; try { sId = Integer.valueOf(request.getParameter("s_id").trim()); } catch (NumberFormatException ne) { throw new HibernateException(new Throwable("The value isn't a number")); } Query query = session.createQuery("from data.S s where s.id = :anId"); query.setParameter("anId", sId); S s = (S) query.uniqueResult(); if (s == null) { throw new HibernateException(new Throwable("This Service ID doesn't exist")); } Iterator it = s.getSrs().iterator(); while (it.hasNext()) { session.delete((Sr) it.next()); } session.persist(s); trans.commit(); session.close(); out.println("The Service Registery(ies) has(have) been removed !!<br/>"); } catch (HibernateException he) { out.println(he.getMessage() + "<br/>"); //out.println(he.getCause() + "<br/>"); out.println("Failed to delete the Service Registery(ies)...<br/>"); trans.rollback(); session.close(); } out.println("</body>"); out.println("</html>"); } }
From source file:servlet.FindSC.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.// ww w .j av a 2 s . co m * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet FindSC</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet FindSC at " + request.getContextPath() + "</h1>"); // No try catch, I suppose the connexion will be okey... Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build()); Session session = sessionFactory.openSession(); try { String sc_name = '%' + request.getParameter("sc_name").trim() + '%'; Criteria crit = session.createCriteria(Sr.class); crit.add(Restrictions.like("scName", sc_name)); Iterator it = crit.list().iterator(); Set<Sr> srs = new HashSet(); while (it.hasNext()) { srs.add((Sr) it.next()); } request.setAttribute("srs", srs); session.close(); getServletConfig().getServletContext().getRequestDispatcher("/FindSC.jsp").forward(request, response); } catch (HibernateException he) { out.println(he.getMessage() + "<br/>"); session.close(); } out.println("</body>"); out.println("</html>"); } }
From source file:servlet.NewQoso.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*ww w .j a va 2 s. co m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet NewQoso</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet NewQoso at " + request.getContextPath() + "</h1>"); // No try catch, I suppose the connexion will be okey... Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build()); Session session = sessionFactory.openSession(); Transaction trans = session.beginTransaction(); try { Qoso qoso = new Qoso(); session.persist(qoso); trans.commit(); session.close(); out.println("The creation of this Quality of Service Offered has succeded !!<br/>"); } catch (HibernateException he) { out.println(he.getMessage() + "<br/>"); //out.println(he.getCause() + "<br/>"); out.println("The creation of this Quality of Service Offered has failed...<br/>"); trans.rollback(); session.close(); } out.println("</body>"); out.println("</html>"); } }
From source file:servlet.NewS.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w ww . ja va2 s.c o m * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet NewS</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet NewS at " + request.getContextPath() + "</h1>"); // No try catch, I suppose the connexion will be okey... Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build()); Session session = sessionFactory.openSession(); Transaction trans = session.beginTransaction(); try { //String id = request.getParameter("s_id").trim(); String name = request.getParameter("s_name").trim(); Query query = session.createQuery("from data.S s where s.name = :aName"); query.setParameter("aName", name); if ((S) query.uniqueResult() != null) { throw new HibernateException(new Throwable("This Service name already exists")); } S s = new S(); s.setName(name); session.persist(s); trans.commit(); session.close(); out.println("The creation of this Service has succeded !!<br/>"); } catch (HibernateException he) { out.println(he.getMessage() + "<br/>"); //out.println(he.getCause() + "<br/>"); out.println("The creation of this Service has failed...<br/>"); trans.rollback(); session.close(); } out.println("</body>"); out.println("</html>"); } }
From source file:servlet.NewSC.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w ww . j av a 2 s . co m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet NewSC</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet NewSC at " + request.getContextPath() + "</h1>"); // No try catch, I suppose the connexion will be okey... Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build()); Session session = sessionFactory.openSession(); Transaction trans = session.beginTransaction(); try { //String id = request.getParameter("sc_id").trim(); String name = request.getParameter("sc_name").trim(); Query query = session.createQuery("from data.Sc sc where sc.name = :aName"); query.setParameter("aName", name); if ((Sc) query.uniqueResult() != null) { throw new HibernateException(new Throwable("This Service Capability name already exists")); } Sc sc = new Sc(); sc.setName(name); /* if (!id.equals("")) { try { int newId = Integer.valueOf(id); // Search if this id isn't already used Query query = session.createQuery("from data.Sc sc where sc.id = :anId"); query.setParameter("anId", newId); if ((Sc) query.uniqueResult() == null) { // This id doesn't exist yet sc.setId(newId); } else { throw new HibernateException(new Throwable("This ID already exist")); } } catch (NumberFormatException ne) { throw new HibernateException(new Throwable("This ID isn't an integer")); } } */ session.persist(sc); trans.commit(); session.close(); out.println("The creation has succeded !!<br/>"); } catch (HibernateException he) { out.println(he.getMessage() + "<br/>"); //out.println(he.getCause() + "<br/>"); out.println("The creation has failed...<br/>"); trans.rollback(); session.close(); } out.println("</body>"); out.println("</html>"); } }
From source file:servlet.NewSP.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.// ww w .j a v a 2 s .c om * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet NewSP</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet NewSP at " + request.getContextPath() + "</h1>"); // No try catch, I suppose the connexion will be okey... Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build()); Session session = sessionFactory.openSession(); Transaction trans = session.beginTransaction(); try { Sp sp = new Sp(); session.persist(sp); trans.commit(); session.close(); out.println("The creation of this Service P ?? has succeded !!<br/>"); } catch (HibernateException he) { out.println(he.getMessage() + "<br/>"); //out.println(he.getCause() + "<br/>"); out.println("The creation of this Service P ?? has failed...<br/>"); trans.rollback(); session.close(); } out.println("</body>"); out.println("</html>"); } }
From source file:servlet.NewSR.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w w w .ja v a2 s. c o m * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet page1</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet page1 at " + request.getContextPath() + "</h1>"); try { Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build()); Session session = sessionFactory.openSession(); Transaction trans = session.beginTransaction(); Query query = session.createQuery("from data.Sc sc where sc.id = :anId"); query.setParameter("anId", Integer.valueOf(request.getParameter("sc_id"))); Sc sc = (Sc) query.uniqueResult(); if (sc == null) { throw new HibernateException(new Throwable("The SC_ID doesn't exist")); } query = session.createQuery("from data.S s where s.id = :anId"); query.setParameter("anId", Integer.valueOf(request.getParameter("s_id"))); S s = (S) query.uniqueResult(); if (sc == null) { throw new HibernateException(new Throwable("The S_ID doesn't exist")); } query = session.createQuery("from data.Sp sp where sp.id = :anId"); query.setParameter("anId", Integer.valueOf(request.getParameter("sp_id"))); Sp sp = (Sp) query.uniqueResult(); if (sp == null) { throw new HibernateException(new Throwable("The SP_ID doesn't exist")); } query = session.createQuery("from data.Qoso qoso where qoso.id = :anId"); query.setParameter("anId", Integer.valueOf(request.getParameter("qoso_id"))); Qoso qoso = (Qoso) query.uniqueResult(); if (qoso == null) { throw new HibernateException(new Throwable("The Qoso_ID doesn't exist")); } Sr sr = new Sr(qoso, s, sc, sp, sc.getName(), s.getName(), request.getParameter("statut")); session.persist(sr); trans.commit(); session.close(); out.println("The creation of this Service Registery has succeded !!<br/>"); } catch (HibernateException he) { out.println(he.getMessage() + "<br/>"); out.println("The creation of this Service Registery has failed...<br/>"); } out.println("</body>"); out.println("</html>"); } }
From source file:servlet.UpdateSRStatut.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.// w w w .jav a 2s. c o m * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet UpdateSRStatut</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet UpdateSRStatut at " + request.getContextPath() + "</h1>"); // No try catch, I suppose the connexion will be okey... Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build()); Session session = sessionFactory.openSession(); Transaction trans = session.beginTransaction(); try { int sr_id; try { sr_id = Integer.valueOf(request.getParameter("sr_id")); } catch (NumberFormatException ne) { throw new HibernateException(new Throwable("The SR ID isn't a number")); } Query query = session.createQuery("from data.Sr sr where sr.id = :anId"); query.setParameter("anId", sr_id); Sr sr = (Sr) query.uniqueResult(); if (sr == null) { throw new HibernateException(new Throwable("The SR ID doesn't exist")); } sr.setStatus(request.getParameter("statut")); session.persist(sr); trans.commit(); session.close(); out.println("The update of the statut has succeded !!<br/>"); } catch (HibernateException he) { out.println(he.getMessage() + "<br/>"); out.println("The update of the statut has failed...<br/>"); trans.rollback(); session.close(); } out.println("</body>"); out.println("</html>"); } }