public static SessionFactory getSessionFactory() { if (null != sessionFactory) return sessionFactory; Configuration configuration = new Configuration(); String jdbcUrl = "jdbc:mysql://" + System.getenv("RDS_HOSTNAME") + "/" + System.getenv("RDS_DB_NAME"); configuration.setProperty("hibernate.connection.url", jdbcUrl); configuration.setProperty("hibernate.connection.username", System.getenv("RDS_USERNAME")); configuration.setProperty("hibernate.connection.password", System.getenv("RDS_PASSWORD")); configuration.configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); try { sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (HibernateException e) { System.err.println("Initial SessionFactory creation failed." + e); throw new ExceptionInInitializerError(e); } return sessionFactory; }
private SessionFactory createSessionFactory(boolean traceWithActiveSpanOnly) { Configuration configuration = new Configuration(); configuration.addAnnotatedClass(Employee.class); configuration.setProperty("hibernate.connection.driver_class", "io.opentracing.contrib.jdbc.TracingDriver"); configuration.setProperty("hibernate.connection.url", "jdbc:tracing:h2:mem:hibernate?traceWithActiveSpanOnly=" + traceWithActiveSpanOnly); configuration.setProperty("hibernate.connection.username", "sa"); configuration.setProperty("hibernate.connection.password", ""); configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); configuration.setProperty("hibernate.hbm2ddl.auto", "create-drop"); configuration.setProperty("hibernate.show_sql", "true"); configuration.setProperty("hibernate.connection.pool_size", "10"); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); return configuration.buildSessionFactory(builder.build()); }
private SessionFactory createSessionFactory(String options) { Configuration configuration = new Configuration(); configuration.addAnnotatedClass(Employee.class); configuration.setProperty("hibernate.connection.url", "jdbc:p6spy:hsqldb:mem:hibernate" + options); configuration.setProperty("hibernate.connection.username", "sa"); configuration.setProperty("hibernate.connection.password", ""); configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); configuration.setProperty("hibernate.hbm2ddl.auto", "create-drop"); configuration.setProperty("hibernate.show_sql", "true"); configuration.setProperty("hibernate.connection.pool_size", "10"); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build()); return sessionFactory; }
@BeforeClass protected void setUp() throws Exception { // A SessionFactory is set up once for an application! final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() // configures settings from hibernate.cfg.xml .build(); try { sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); } catch (Exception e) { e.printStackTrace(); // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory // so destroy it manually. StandardServiceRegistryBuilder.destroy( registry ); } }
private static void init() { // 从hibernate.cfg.xml文件初始化 final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() // configures settings from hibernate.cfg.xml .build(); try { // build 一个sessionFactory sessionFactory = new MetadataSources(registry) .buildMetadata() .buildSessionFactory(); } catch (Exception e) { e.printStackTrace(); // 错误则打印输出,并销毁 StandardServiceRegistryBuilder.destroy(registry); } }
private static StandardServiceRegistry getStandardServiceRegistry(ServiceRegistry serviceRegistry) { if ( serviceRegistry == null ) { throw new HibernateException( "ServiceRegistry passed to MetadataBuilder cannot be null" ); } if ( StandardServiceRegistry.class.isInstance( serviceRegistry ) ) { return ( StandardServiceRegistry ) serviceRegistry; } else if ( BootstrapServiceRegistry.class.isInstance( serviceRegistry ) ) { log.debugf( "ServiceRegistry passed to MetadataBuilder was a BootstrapServiceRegistry; this likely wont end well" + "if attempt is made to build SessionFactory" ); return new StandardServiceRegistryBuilder( (BootstrapServiceRegistry) serviceRegistry ).build(); } else { throw new HibernateException( String.format( "Unexpected type of ServiceRegistry [%s] encountered in attempt to build MetadataBuilder", serviceRegistry.getClass().getName() ) ); } }
public HibernateUserDao() { // hibernate5 final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure() // configures .build(); try { sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); } catch (Exception e) { // The registry would be destroyed by the SessionFactory, but we had // trouble building the SessionFactory // so destroy it manually. StandardServiceRegistryBuilder.destroy(registry); } // hibernate4 // try { // // Create the SessionFactory from hibernate.cfg.xml // sessionFactory = new // Configuration().configure().buildSessionFactory(); // } 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); // } }
public static SessionFactory getSessionFactory() { if (sessionFactory == null) { StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() // configures settings from hibernate.cfg.xml .build(); try { sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); } catch (Exception e) { // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory // so destroy it manually. StandardServiceRegistryBuilder.destroy(registry); throw new RuntimeException(e); } } return sessionFactory; }
public static SessionFactory getSessionFactory() { if (sessionFactory == null || sessionFactory.isClosed()) { try { Configuration configuration = new Configuration().configure(); StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder(); serviceRegistryBuilder.applySettings(configuration.getProperties()); ServiceRegistry serviceRegistry = serviceRegistryBuilder.build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); // sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (HibernateException ex) { ex.printStackTrace(); System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } return sessionFactory; }
@BeforeClass protected void setUp() throws Exception { // A SessionFactory is set up once for an application! final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() // configures settings from hibernate.cfg.xml .build(); try { sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); } catch (Exception e) { e.printStackTrace(); // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory // so destroy it manually. StandardServiceRegistryBuilder.destroy( registry ); } }
@Override public void start() { StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .applySetting("hibernate.connection.username", config.persistence.user) .applySetting("hibernate.connection.password", config.persistence.pass) .applySetting("hibernate.connection.driver_class", config.persistence.driver) .applySetting("hibernate.connection.url", config.persistence.url) .applySetting("hibernate.dialect", config.persistence.dialect) .applySetting("hibernate.connection.pool_size", config.persistence.pool_size + "") .applySetting("hibernate.hbm2ddl.auto", "update") .applySetting("hibernate.show_sql", config.persistence.showSQL + "") .build(); MetadataSources sources = new MetadataSources(registry); Timings.time("RegisterDBEntities", () -> new Reflections().getTypesAnnotatedWith(Entity.class).forEach(sources::addAnnotatedClass)); try { Metadata metadata = sources.buildMetadata(); sessionFactory = metadata.buildSessionFactory(); } catch (Exception e) { StandardServiceRegistryBuilder.destroy(registry); e.printStackTrace(); } }
/** * Created by admin on 10.01.2017. * @return SessionFactory */ protected static SessionFactory buildSessionFactory() { // A SessionFactory is set up once for an application! final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() // configures settings from hibernate.cfg.xml .build(); try { sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); } catch (Exception e) { // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory // so destroy it manually. StandardServiceRegistryBuilder.destroy(registry); throw new ExceptionInInitializerError("Initial SessionFactory failed" + e); } return sessionFactory; }
/** * Created by admin on 30.12.2016. * create factory * @return SessionFactory */ protected static SessionFactory buildSessionFactory() { // A SessionFactory is set up once for an application! final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() // configures settings from hibernate.cfg.xml .build(); try { sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); } catch (Exception e) { // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory // so destroy it manually. StandardServiceRegistryBuilder.destroy(registry); throw new ExceptionInInitializerError("Initial SessionFactory failed" + e); } return sessionFactory; }
public static SessionFactory getSessionFactory() { final Configuration configuration = new Configuration().configure(); configuration.addPackage("com.tvajjala.domain"); configuration.addAnnotatedClass(com.tvajjala.domain.User.class); configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/test"); configuration.setProperty("hibernate.connection.username", "root"); configuration.setProperty("hibernate.connection.password", "admin"); System.out.println("Using dialect " + org.hibernate.dialect.MySQL5InnoDBDialect.class.getName()); configuration.setProperty("hibernate.dialect", org.hibernate.dialect.MySQL5InnoDBDialect.class.getName()); configuration.setProperty("hibernate.hbm2ddl.auto", "update"); // session_context_class is jta then transaction manager lookup class need to set // configuration.setProperty("hibernate.transaction.manager_lookup_class", TransactionManagerLookup.); // This below property used to create session per context// jta, thread, managed configuration.setProperty("hibernate.current_session_context_class", "thread"); configuration.setProperty("hibernate.show_sql", "true"); configuration.setProperty(" hibernate.connection.pool_size", "10"); final StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); return configuration.buildSessionFactory(builder.build()); }
public static SessionFactory buildSessionFactory() { try { Configuration configuration = new Configuration(); // Create properties file Properties properties = new Properties(); properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("Hibernate.properties")); configuration.setProperties(properties); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); configuration.addAnnotatedClass(Wordlist.class); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Throwable ex) { ex.printStackTrace(); } return sessionFactory; }
private static SessionFactory buildSessionFactory() { try { Configuration configuration = new Configuration(); Properties props = new Properties(); props.put("hibernate.current_session_context_class", "thread"); configuration.setProperties(props); for (Class<?> clazz : entityClasses) { configuration.addAnnotatedClass(clazz); } PluginHandler.get().getPlugin().addAnnotatedClass(configuration); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()) .addService(ConnectionProvider.class, new NomadConnectionProvider()).build(); SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); return sessionFactory; } catch (Throwable ex) { logger.error("Failed to build session factory.", ex); throw new ExceptionInInitializerError(ex); } }
private SessionFactory buildSessionFactory(String dbName) { Configuration configuration = new Configuration(); configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); configuration.setProperty("hibernate.connection.driver_class", "org.h2.Driver"); configuration.setProperty("hibernate.connection.url", "jdbc:h2:mem:" + dbName); configuration.setProperty("hibernate.hbm2ddl.auto", "create"); configuration.setProperty("hibernate.current_session_context_class", "managed"); configuration.addAnnotatedClass(TestEntity.class); configuration.addAnnotatedClass(Phone.class); configuration.addAnnotatedClass(Transaction.class); configuration.addAnnotatedClass(Audit.class); StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings( configuration.getProperties()).build(); return configuration.buildSessionFactory(serviceRegistry); }
private SessionFactory buildSessionFactory(String dbName) { Configuration configuration = new Configuration(); configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); configuration.setProperty("hibernate.connection.driver_class", "org.h2.Driver"); configuration.setProperty("hibernate.connection.url", "jdbc:h2:mem:" + dbName); configuration.setProperty("hibernate.hbm2ddl.auto", "create"); configuration.setProperty("hibernate.current_session_context_class", "managed"); configuration.addAnnotatedClass(Order.class); configuration.addAnnotatedClass(OrderItem.class); StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings( configuration.getProperties()).build(); return configuration.buildSessionFactory(serviceRegistry); }
private SessionFactory buildSessionFactory(String dbName) { Configuration configuration = new Configuration(); configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); configuration.setProperty("hibernate.connection.driver_class", "org.h2.Driver"); configuration.setProperty("hibernate.connection.url", "jdbc:h2:mem:" + dbName); configuration.setProperty("hibernate.hbm2ddl.auto", "create"); configuration.setProperty("hibernate.current_session_context_class", "managed"); configuration.addAnnotatedClass(RelationalEntity.class); StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings( configuration.getProperties()).build(); return configuration.buildSessionFactory(serviceRegistry); }
@Test public void bootstrapping() { log.info("... bootstrapping ..."); ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build(); SessionFactory sessionFactory = new MetadataSources(standardRegistry) .addAnnotatedClass(Author.class).buildMetadata() .buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); Author a = new Author(); a.setFirstName("Thorben"); a.setLastName("Janssen"); session.persist(a); session.getTransaction().commit(); session.close(); }
public boolean addRegex(Regex regex) { Transaction tx = null; Configuration config = new Configuration().configure(); config.addClass(Regex.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()) .build(); SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); Session session = sessionFactory.getCurrentSession(); try{ tx = session.beginTransaction(); session.save(regex); tx.commit(); return true; }catch (Exception ex){ ex.printStackTrace(); tx.commit(); return false; }finally { if(tx != null){ tx = null; } sessionFactory.close(); } }
public boolean updateRegex(Regex regex) { Transaction tx = null; Configuration config = new Configuration().configure(); config.addClass(Regex.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()) .build(); SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); Session session = sessionFactory.getCurrentSession(); try{ tx = session.beginTransaction(); session.update(regex); tx.commit(); return true; }catch (Exception ex){ ex.printStackTrace(); tx.commit(); return false; }finally { if(tx != null){ tx = null; } sessionFactory.close(); } }
public boolean addData(Data data) { Transaction tx = null; Configuration config = new Configuration().configure(); config.addClass(Data.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()) .build(); SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); Session session = sessionFactory.getCurrentSession(); try{ tx = session.beginTransaction(); session.save(data); tx.commit(); return true; }catch (Exception ex){ ex.printStackTrace(); tx.commit(); return false; }finally { if(tx != null){ tx = null; } sessionFactory.close(); } }
public boolean updateData(Data data) { Transaction tx = null; Configuration config = new Configuration().configure(); config.addClass(Data.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()) .build(); SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); Session session = sessionFactory.getCurrentSession(); try{ tx = session.beginTransaction(); session.update(data); tx.commit(); return true; }catch (Exception ex){ ex.printStackTrace(); tx.commit(); return false; }finally { if(tx != null){ tx = null; } sessionFactory.close(); } }
public boolean addTask(Task task) { Transaction tx = null; Configuration config = new Configuration().configure(); config.addClass(Task.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()) .build(); SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); Session session = sessionFactory.getCurrentSession(); try{ tx = session.beginTransaction(); session.save(task); tx.commit(); return true; }catch (Exception ex){ ex.printStackTrace(); tx.commit(); return false; }finally { if(tx != null){ tx = null; } sessionFactory.close(); } }
public boolean updateTask(Task task) { Transaction tx = null; Configuration config = new Configuration().configure(); config.addClass(Task.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()) .build(); SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); Session session = sessionFactory.getCurrentSession(); try{ tx = session.beginTransaction(); session.update(task); tx.commit(); return true; }catch (Exception ex){ ex.printStackTrace(); tx.commit(); return false; }finally { if(tx != null){ tx = null; } sessionFactory.close(); } }
public boolean addWorkspace(Workspace wspace) { Transaction tx = null; Configuration config = new Configuration().configure(); config.addClass(Workspace.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()) .build(); SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); Session session = sessionFactory.getCurrentSession(); try{ tx = session.beginTransaction(); session.save(wspace); tx.commit(); return true; }catch (Exception ex){ ex.printStackTrace(); tx.commit(); return false; }finally { if(tx != null){ tx = null; } sessionFactory.close(); } }
public boolean updateWorkspace(Workspace wspace) { Transaction tx = null; Configuration config = new Configuration().configure(); config.addClass(Workspace.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()) .build(); SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); Session session = sessionFactory.getCurrentSession(); try{ tx = session.beginTransaction(); session.update(wspace); tx.commit(); return true; }catch (Exception ex){ ex.printStackTrace(); tx.commit(); return false; }finally { if(tx != null){ tx = null; } sessionFactory.close(); } }
public boolean addProject(Project p) { Transaction tx = null; Configuration config = new Configuration().configure(); config.addClass(Project.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()) .build(); SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); Session session = sessionFactory.getCurrentSession(); try{ tx = session.beginTransaction(); session.save(p); tx.commit(); return true; }catch (Exception ex){ ex.printStackTrace(); tx.commit(); return false; }finally { if(tx != null){ tx = null; } sessionFactory.close(); } }
public boolean updateProject(Project p) { Transaction tx = null; Configuration config = new Configuration().configure(); config.addClass(Project.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()) .build(); SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); Session session = sessionFactory.getCurrentSession(); try{ tx = session.beginTransaction(); session.update(p); tx.commit(); return true; }catch (Exception ex){ ex.printStackTrace(); tx.commit(); return false; }finally { if(tx != null){ tx = null; } sessionFactory.close(); } }
@BeforeClass public void setUpDb() throws Exception { // A SessionFactory is set up once for an application! final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() // configures settings from hibernate.cfg.xml .build(); try { sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); } catch (Exception e) { e.printStackTrace(); // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory // so destroy it manually. StandardServiceRegistryBuilder.destroy( registry ); } }
public static SessionFactory getSessionFactory() { log.info("+++ getSessionFactory"); if (sf == null) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); // @formatter:off cfg.addPackage("pl.java.scalatech.domain") .addAnnotatedClass(Item.class) .addAnnotatedClass(GeneratorOne.class) .addAnnotatedClass(Note.class) .addAnnotatedClass(HiloEntity.class) .addAnnotatedClass(TableGeneratorEntity.class) ; // @formatter:on cfg.setProperty("hibernate.generate_statistics","true"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build(); sf = cfg.buildSessionFactory(serviceRegistry); } return sf; }
public static SessionFactory getSessionFactory(Class[] clazz) { log.info("+++ getConcreteEntitySessionFactory"); if (sf == null) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); // @formatter:off for (Class cl : clazz) { log.info("+++ mapped entity => {}",cl); cfg.addPackage("pl.java.scalatech.domain").addAnnotatedClass(cl); } // @formatter:on cfg.setProperty("hibernate.generate_statistics","true"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build(); sf = cfg.buildSessionFactory(serviceRegistry); } return sf; }