List of usage examples for org.hibernate.boot.registry StandardServiceRegistryBuilder build
@SuppressWarnings("unchecked") public StandardServiceRegistry build()
From source file:org.wallride.tools.Hbm2ddl.java
License:Apache License
public static void main(String[] args) throws Exception { String locationPattern = "classpath:/org/wallride/domain/*"; final BootstrapServiceRegistry registry = new BootstrapServiceRegistryBuilder().build(); final MetadataSources metadataSources = new MetadataSources(registry); final StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder(registry); registryBuilder.applySetting(AvailableSettings.DIALECT, ExtendedMySQL5InnoDBDialect.class.getCanonicalName()); registryBuilder.applySetting(AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, true); registryBuilder.applySetting(AvailableSettings.PHYSICAL_NAMING_STRATEGY, PhysicalNamingStrategySnakeCaseImpl.class); final PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); final Resource[] resources = resourcePatternResolver.getResources(locationPattern); final SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); for (Resource resource : resources) { MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); if (metadata.hasAnnotation(Entity.class.getName())) { metadataSources.addAnnotatedClass(Class.forName(metadata.getClassName())); }/* w w w . java 2 s. c om*/ } final StandardServiceRegistryImpl registryImpl = (StandardServiceRegistryImpl) registryBuilder.build(); final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder(registryImpl); new SchemaExport().setHaltOnError(true).setDelimiter(";").create(EnumSet.of(TargetType.STDOUT), metadataBuilder.build()); }
From source file:osdigital.dao.HibernateUtil.java
private static SessionFactory buildSessionFactory() { try {/*from w w w. jav a2 s.co m*/ Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder registradorServico = new StandardServiceRegistryBuilder(); registradorServico.applySettings(cfg.getProperties()); StandardServiceRegistry servico = registradorServico.build(); return cfg.buildSessionFactory(servico); } catch (Throwable e) { // TODO: handle exception System.out.println("Criao inicial do Session Falhou. Erro !!" + e); throw new ExceptionInInitializerError(e); } }
From source file:principal.HibernateUtil.java
private static SessionFactory buildSessionFactory() { try {/* w w w . j a v a 2 s . co m*/ if (sessionFactory == null) { Configuration configuration = new Configuration() .configure(HibernateUtil.class.getResource("/hibernate.cfg.xml")); StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder(); serviceRegistryBuilder.applySettings(configuration.getProperties()); ServiceRegistry serviceRegistry = serviceRegistryBuilder.build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } return sessionFactory; } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed: " + ex); throw new ExceptionInInitializerError(ex); } }
From source file:prod.service.HibernateUtil.java
private static SessionFactory buildSessionFactory() { try {//from w w w .j av a2s .c om if (sessionFactory == null) { Configuration configuration = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); sessionFactory = configuration.buildSessionFactory(builder.build()); configuration.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false"); } return sessionFactory; } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } }
From source file:servlet.DeleteSR.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.// w w w . j a v a 2 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.//w w w.j a v a 2 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 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./* w w 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 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 ww w .j ava 2 s . com * * @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 w w. ja v a2s . com * * @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./*from w w w. ja va 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>"); } }