@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); } }
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; }
@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; }
@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(); }
@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 ); } }
@Before public void setup() { StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder() .applySetting( "hibernate.show_sql", "true" ) .applySetting( "hibernate.format_sql", "true" ) .applySetting( "hibernate.hbm2ddl.auto", "update" ) .applySetting( "hibernate.dialect", "org.hibernate.dialect.H2Dialect" ) .applySetting( "hibernate.connection.driver_class", "org.h2.Driver" ) .applySetting( "hibernate.connection.url", "jdbc:h2:mem:testdbHibernate" ) .applySetting( "hibernate.connection.username", "sa" ) .applySetting( "hibernate.connection.password", "" ) .applySetting( "hibernate.use_sql_comment", "true" ) ; Metadata metadata = null; if(packageBase()== null){ metadata= new MetadataSources( srb.build() ).addAnnotatedClass(getEntityClass()).buildMetadata(); }else{ metadata= new MetadataSources( srb.build() ) .addAnnotatedClass(Item.class) .addAnnotatedClass(Offer.class) .addPackage(packageBase()).buildMetadata(); } sf = metadata.buildSessionFactory(); }
@BeforeClass public static void setup() { // @formatter:off StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder() .applySetting("hibernate.show_sql", "true") .applySetting("hibernate.format_sql", "true") .applySetting("hibernate.hbm2ddl.auto", "update") .applySetting("use_sql_comments", "true") .applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect") .applySetting("hibernate.connection.driver_class", "org.h2.Driver") .applySetting("hibernate.connection.url", "jdbc:h2:mem:testdbHibernate") .applySetting("hibernate.connection.username", "sa") .applySetting("hibernate.connection.password", "") .applySetting("hibernate.use_sql_comment", "true"); Metadata metadata = null; log.info("+++ setup"); metadata = new MetadataSources(srb.build()).addAnnotatedClass(EmailMessage.class).addAnnotatedClass(Email.class).buildMetadata(); sf = metadata.buildSessionFactory(); // @formatter:on }
@BeforeClass public static void setup() { // @formatter:off StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder() .applySetting("hibernate.show_sql", "true") .applySetting("hibernate.format_sql", "true") .applySetting("hibernate.hbm2ddl.auto", "update") .applySetting("use_sql_comments", "true") .applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect") .applySetting("hibernate.connection.driver_class", "org.h2.Driver") .applySetting("hibernate.connection.url", "jdbc:h2:mem:testdbHibernate") .applySetting("hibernate.connection.username", "sa") .applySetting("hibernate.connection.password", "") .applySetting("hibernate.use_sql_comment", "true"); Metadata metadata = null; log.info("+++ setup"); metadata = new MetadataSources(srb.build()).addAnnotatedClass(Address.class).addAnnotatedClass(User.class).buildMetadata(); sf = metadata.buildSessionFactory(); // @formatter:on }
public HibernateDatabase(Properties properties) throws HibernateException { StandardServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(properties).build(); MetadataSources metadataSources = new MetadataSources(registry); metadataSources.addAnnotatedClass(SystemInfo.class) .addAnnotatedClass(Project.class) .addAnnotatedClass(Runner.class) .addAnnotatedClass(Repository.class) .addAnnotatedClass(RepositoryType.class) .addAnnotatedClass(SystemUnderTest.class) .addAnnotatedClass(Requirement.class) .addAnnotatedClass(Specification.class) .addAnnotatedClass(Reference.class) .addAnnotatedClass(Execution.class); this.properties = properties; this.metadata = metadataSources.buildMetadata(); }
public static SqmDomainMetamodel buildDomainMetamodel(Class... managedClasses) { final MetadataSources metadataSources = new MetadataSources(); for ( Class managedClass : managedClasses ) { metadataSources.addAnnotatedClass( managedClass ); } final MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata(); metadata.validate(); // at this point we have access to the mapping Metadata (PersistentClass, etc) // use it to build the testing DomainMetamodel/TypeConfiguration final ExplicitSqmDomainMetamodel domainMetamodel = new ExplicitSqmDomainMetamodel( metadata ); populateDatabaseModel( metadata, domainMetamodel ); populateMappingModel( metadata, domainMetamodel ); return domainMetamodel; }
/** * @param nodeName Name of the grid providing caches. * @return Session factory. */ SessionFactory startHibernate(String nodeName) { log.info("Start hibernate on node: " + nodeName); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(); for (Map.Entry<String, String> e : hibernateProperties(nodeName, NONSTRICT_READ_WRITE.name()).entrySet()) builder.applySetting(e.getKey(), e.getValue()); builder.applySetting("hibernate.connection.url", CONNECTION_URL); builder.applySetting(DFLT_CACHE_NAME_PROPERTY, CACHE_NAME); MetadataSources metadataSources = new MetadataSources(builder.build()); metadataSources.addAnnotatedClass(Entity1.class); metadataSources.addAnnotatedClass(Entity2.class); metadataSources.addAnnotatedClass(Entity3.class); return metadataSources.buildMetadata().buildSessionFactory(); }
protected void export(Class<? extends Dialect> dialect, String app_key, MetadataSources metadata, boolean create, boolean drop) { Assert.notNull(dialect, "dialect is invalid [null]"); Assert.notNull(app_key, "app_key is invalid [null]"); Assert.notNull(metadata, "metadata is invalid [null]"); SchemaExport schemaExport = new SchemaExport((MetadataImplementor) metadata.buildMetadata()); schemaExport.setDelimiter(";"); schemaExport.setFormat(true); if (create) { // Generate create script schemaExport.setOutputFile(String.format("target/%s_ddl_%s_create.sql", app_key, dialect.getSimpleName())); schemaExport.execute(true, false, false, true); } if (drop) { // Generate drop script schemaExport.setOutputFile(String.format("target/%s_ddl_%s_drop.sql", app_key, dialect.getSimpleName())); schemaExport.execute(true, false, true, false); } }
/** * Method that actually creates the file. * * @param dbDialect to use */ private void generate(Dialect dialect) { StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(); ssrb.applySetting("hibernate.dialect", dialect.getDialectClass()); StandardServiceRegistry standardServiceRegistry = ssrb.build(); MetadataSources metadataSources = new MetadataSources(standardServiceRegistry); for (Class clzz : jpaClasses) { metadataSources.addAnnotatedClass(clzz); } Metadata metadata = metadataSources.buildMetadata(); SchemaExport export = new SchemaExport(); export.setDelimiter(";"); export.setOutputFile(dialect.name().toLowerCase() + ".ddl"); //export.execute(true, false, false, true); export.execute(EnumSet.of(TargetType.SCRIPT), Action.BOTH, metadata); }
/** * Provide a singleton instance of the hibernate session factory. * * @return A session factory. */ @Override public SessionFactory provide() { logger.trace("Creating hibernate session factory."); // Build the service registry. SessionFactory factory = new MetadataSources(serviceRegistry) .buildMetadata() .buildSessionFactory(); // Register our event listeners. injectEventListeners(((SessionFactoryImpl) factory) .getServiceRegistry()); return factory; }
/** * Test provide and dispose. */ @Test public void testProvideDispose() { HibernateServiceRegistryFactory factory = new HibernateServiceRegistryFactory(); ServiceRegistry serviceRegistry = factory.provide(); Dialect d = new MetadataSources(serviceRegistry) .buildMetadata().getDatabase().getDialect(); Assert.assertTrue(d instanceof H2Dialect); // This shouldn't actually do anything, but is included here for // coverage. factory.dispose(serviceRegistry); }
/** * Test the application binder. */ @Test public void testBinder() { ResourceConfig config = new ResourceConfig(); config.register(TestFeature.class); // Make sure it's registered Assert.assertTrue(config.isRegistered(TestFeature.class)); // Create a fake application. ApplicationHandler handler = new ApplicationHandler(config); ServiceRegistry serviceRegistry = handler .getServiceLocator().getService(ServiceRegistry.class); Assert.assertNotNull(serviceRegistry); // Make sure it's reading from the same place. Dialect d = new MetadataSources(serviceRegistry) .buildMetadata().getDatabase().getDialect(); Assert.assertTrue(d instanceof H2Dialect); // Make sure it's a singleton... ServiceRegistry serviceRegistry2 = handler .getServiceLocator().getService(ServiceRegistry.class); Assert.assertSame(serviceRegistry, serviceRegistry2); }
public static MetadataImplementor createMetadataImplementor(String dialect, String[] hibernatePackagesToScan) { final MetadataSources metadata = new MetadataSources( new StandardServiceRegistryBuilder() .applySetting("hibernate.dialect", dialect) .build()); for (String packageName : hibernatePackagesToScan) { for (Class clazz : HibernateMetaDataHelper.getClasses(packageName)) { metadata.addAnnotatedClass(clazz); } } return (MetadataImplementor) metadata.buildMetadata(); }
@Before public void setUp() throws Exception { StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect") .applySetting("hibernate.connection.url", "jdbc:h2:mem:test") .applySetting("hibernate.hbm2ddl.auto", "create-drop") .build(); sessionFactory = new MetadataSources(serviceRegistry) .addAnnotatedClass(CountryData.class) .addAnnotatedClass(CityData.class) .buildMetadata() .buildSessionFactory(); }
public DbHelper() { // A SessionFactory is set up once for an application! final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure("hibernate.cfg.xml") // configures settings from hibernate.cfg.xml .build(); sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); }
public DBServiceImpl() { StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder(); Map<String, Object> settings = new HashMap<>(); settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect"); settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver"); settings.put(Environment.URL, "jdbc:mysql://localhost:3306/db_example"); settings.put(Environment.USER, "tully"); settings.put(Environment.PASS, "tully"); settings.put(Environment.HBM2DDL_AUTO, "create"); settings.put(Environment.SHOW_SQL, true); settings.put(Environment.ENABLE_LAZY_LOAD_NO_TRANS, true); // c3p0 configuration settings.put(Environment.C3P0_MIN_SIZE, 5); //Minimum size of pool settings.put(Environment.C3P0_MAX_SIZE, 20); //Maximum size of pool settings.put(Environment.C3P0_ACQUIRE_INCREMENT, 1);//Number of connections acquired at a time when pool is exhausted settings.put(Environment.C3P0_TIMEOUT, 1800); //Connection idle time registryBuilder.applySettings(settings); ServiceRegistry registry = registryBuilder.build(); MetadataSources sources = new MetadataSources(registry) .addAnnotatedClass(PhoneDataSet.class) .addAnnotatedClass(UserDataSet.class) .addAnnotatedClass(AddressDataSet.class); Metadata metadata = sources.getMetadataBuilder().build(); sessionFactory = metadata.getSessionFactoryBuilder().build(); }
@Test public void testDbSchema() { MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder() .applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect").build()); metadata.addAnnotatedClass(ConsoleEntity.class); metadata.addAnnotatedClass(GameEntity.class); metadata.addAnnotatedClass(UserEntity.class); metadata.addAnnotatedClass(RoleEntity.class); metadata.addAnnotatedClass(LibraryEntity.class); metadata.addAnnotatedClass(BorrowEntity.class); SchemaExport export = new SchemaExport((MetadataImplementor) metadata.buildMetadata()); export.create(Target.SCRIPT); }
public static void main(String[] args){ String file = "camping-db-creation.sql"; new File(file).delete(); MetadataSources metadata = new MetadataSources( new StandardServiceRegistryBuilder().configure().build()); SchemaExport export = new SchemaExport(); export.setOutputFile(file); export.setDelimiter(";"); export.setFormat(true); export.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, metadata.buildMetadata()); }
public static SessionFactory buildSessionFactory() { 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); } return sessionFactory; }
private DBHelper() { // 创建SessionFactory sf = new MetadataSources(new StandardServiceRegistryBuilder().configure( "hibernate.cfg.xml").build()).buildMetadata().buildSessionFactory(); // 获取Session session = sf.getCurrentSession(); // 获取事务 tx = session.beginTransaction(); }
@BeforeClass public static void init() { // 创建SessionFactory sf = new MetadataSources(new StandardServiceRegistryBuilder().configure( "hibernate.cfg.xml").build()).buildMetadata().buildSessionFactory(); }