List of usage examples for org.hibernate.cfg Configuration getProperties
public Properties getProperties()
From source file:rtdc.web.server.config.PersistenceConfig.java
License:Open Source License
private static SessionFactory buildSessionFactory() { try {/*from ww w . j ava2 s.c om*/ // Create the SessionFactory from prod.hibernate.cfg.xml Configuration configuration = new Configuration().configure("hibernate.cfg.xml") .addResource("User.hbm.xml").addResource("Unit.hbm.xml").addResource("Action.hbm.xml") .addResource("Message.hbm.xml").addAnnotatedClass(UserCredentials.class) .addAnnotatedClass(AuthenticationToken.class); System.out.println("Hibernate Configuration loaded"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); System.out.println("Hibernate serviceRegistry created"); return configuration.buildSessionFactory(serviceRegistry); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } }
From source file:ru.apertum.journal.db.HibernateUtil.java
License:Open Source License
private HibernateUtil() { try {/*from w w w . j a va 2s . com*/ final Configuration configuration = new Configuration(); configuration.configure("/ru/apertum/journal/cfg/hibernate.cfg.xml"); final Properties prop = new Properties(); final File f = new File("config/journal.properties"); if (f.exists()) { prop.load(new FileInputStream(f)); } configuration.addProperties(prop); final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Throwable ex) { // Log exception! throw new ExceptionInInitializerError(ex); } //sessionFactory = new AnnotationConfiguration().addAnnotatedClass(Book.class).buildSessionFactory(); }
From source file:ru.apertum.qsky.ejb.HibernateEJB.java
License:Open Source License
protected SessionFactory getFactory() { if (sessionFactory == null) { try {/*w w w . j a v a 2s . c o m*/ // Create the SessionFactory from standard (hibernate.cfg.xml) // config file. final Configuration configuration = new Configuration(); configuration.configure(); final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Throwable ex) { // Log the exception. System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } return sessionFactory; }
From source file:ru.taximaxim.dbreplicator2.abstracts.AbstractBoneCPTest.java
License:Open Source License
protected static void setUp() throws ClassNotFoundException, SQLException, IOException { Configuration configuration = new Configuration().configure(); // Hibernate sessionFactory = new Configuration().configure().buildSessionFactory( new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry()); // ? ?// ww w . j a v a 2s . c om settingStorage = new BoneCPSettingsService(sessionFactory); }
From source file:ru.taximaxim.dbreplicator2.utils.Core.java
License:Open Source License
/** * ??? .//from w ww . j ava 2 s .co m * * @param configuration - ? ? * @return ??? . */ public static synchronized SessionFactory getSessionFactory(Configuration configuration) { LOG.debug(" ? ??? hibernate"); if (sessionFactory == null) { ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); LOG.info(" ? ??? hibernate"); } return sessionFactory; }
From source file:Search.DeleteMov.java
protected int deleteMovie(String movname) { Configuration configuration = new Configuration(); configuration.configure();/*w ww . j a v a2s . 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 = "DELETE FROM GS_Movie " + "WHERE name = :movname"; Query query = session.createQuery(hql); query.setParameter("movname", movname); result = query.executeUpdate(); System.out.println("Rows affected: " + result); tx.commit(); } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); } finally { session.close(); return result; } }
From source file:Search.GetHistory.java
public static void listHistory() { Configuration configuration = new Configuration(); configuration.configure();/*from www .j a v a 2 s .c o m*/ serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); Session session = new Configuration().configure().buildSessionFactory(serviceRegistry).openSession(); String date; Transaction tx = null; try { tx = session.beginTransaction(); String qry1 = "select hr.date FROM GS_HistoryRes hr"; Query q1 = session.createQuery(qry1); List l1 = q1.list(); ListIterator li1 = l1.listIterator(); //System.out.println(l1); getCount = l1.size(); System.out.println(getCount); strHistory = new String[getCount * 4]; int counter = 0; int iter = 0; try { for (int i = 0; i < getCount; i++) { date = (String) li1.next().toString(); //System.out.println(date); String qry2 = "FROM GS_History h where h.date=:date"; Query q2 = session.createQuery(qry2); q2.setParameter("date", date); List l2 = q2.list(); for (Iterator iterator = l2.iterator(); iterator.hasNext();) { if (iter < 11) { GS_History movieaaa = (GS_History) iterator.next(); //strHistory[counter]=history.getDate()+","+history.getName()+","+history.getPlot()+","+history.getYear(); /* System.out.println(history.getDate()); System.out.println(history.getId()); System.out.println(history.getName()); System.out.println(history.getPlot()); System.out.println(history.getYear());*/ System.out.println(iter); switch (iter) { case 0: moviename1 = movieaaa.getName(); movieplot1 = movieaaa.getPlot(); movieyear1 = String.valueOf(movieaaa.getYear()); System.out.println("Rec Movie name: " + moviename1); System.out.println("Plot: " + movieplot1); System.out.println("Year: " + movieyear1); break; case 1: moviename2 = movieaaa.getName(); movieplot2 = movieaaa.getPlot(); movieyear2 = String.valueOf(movieaaa.getYear()); System.out.println("Rec Movie name: " + moviename2); System.out.println("Plot: " + movieplot2); System.out.println("Year: " + movieyear2); break; case 2: moviename3 = movieaaa.getName(); movieplot3 = movieaaa.getPlot(); movieyear3 = String.valueOf(movieaaa.getYear()); System.out.println("Rec Movie name: " + moviename3); System.out.println("Plot: " + movieplot3); System.out.println("Year: " + movieyear3); break; case 3: moviename4 = movieaaa.getName(); movieplot4 = movieaaa.getPlot(); movieyear4 = String.valueOf(movieaaa.getYear()); System.out.println("Rec Movie name: " + moviename4); System.out.println("Plot: " + movieplot4); System.out.println("Year: " + movieyear4); break; case 4: moviename11 = movieaaa.getName(); movieplot11 = movieaaa.getPlot(); movieyear11 = String.valueOf(movieaaa.getYear()); System.out.println("Rec Movie name: " + moviename11); System.out.println("Plot: " + movieplot11); System.out.println("Year: " + movieyear11); break; case 5: moviename21 = movieaaa.getName(); movieplot21 = movieaaa.getPlot(); movieyear21 = String.valueOf(movieaaa.getYear()); System.out.println("Rec Movie name: " + moviename21); System.out.println("Plot: " + movieplot21); System.out.println("Year: " + movieyear21); break; case 6: moviename31 = movieaaa.getName(); movieplot31 = movieaaa.getPlot(); movieyear31 = String.valueOf(movieaaa.getYear()); System.out.println("Rec Movie name: " + moviename31); System.out.println("Plot: " + movieplot31); System.out.println("Year: " + movieyear31); break; case 7: moviename41 = movieaaa.getName(); movieplot41 = movieaaa.getPlot(); movieyear41 = String.valueOf(movieaaa.getYear()); System.out.println("Rec Movie name: " + moviename41); System.out.println("Plot: " + movieplot41); System.out.println("Year: " + movieyear41); break; case 8: moviename12 = movieaaa.getName(); movieplot12 = movieaaa.getPlot(); movieyear12 = String.valueOf(movieaaa.getYear()); System.out.println("Rec Movie name: " + moviename12); System.out.println("Plot: " + movieplot12); System.out.println("Year: " + movieyear12); break; case 9: moviename22 = movieaaa.getName(); movieplot22 = movieaaa.getPlot(); movieyear22 = String.valueOf(movieaaa.getYear()); System.out.println("Rec Movie name: " + moviename22); System.out.println("Plot: " + movieplot22); System.out.println("Year: " + movieyear22); break; case 10: moviename32 = movieaaa.getName(); movieplot32 = movieaaa.getPlot(); movieyear32 = String.valueOf(movieaaa.getYear()); System.out.println("Rec Movie name: " + moviename32); System.out.println("Plot: " + movieplot32); System.out.println("Year: " + movieyear32); break; case 11: moviename42 = movieaaa.getName(); movieplot42 = movieaaa.getPlot(); movieyear42 = String.valueOf(movieaaa.getYear()); System.out.println("Rec Movie name: " + moviename42); System.out.println("Plot: " + movieplot42); System.out.println("Year: " + movieyear42); break; } } counter++; iter++; } } } catch (Exception e) { System.out.println(e); } } catch (Exception e) { System.out.println(e); } }
From source file:Search.GetMovies.java
public static void listMovies(String movname) { Configuration configuration = new Configuration(); configuration.configure();//from ww w . j a va 2s .c o m 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 ava2s. c o m*/ 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 a 2s. 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 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>"); } }