Java 类org.hibernate.boot.registry.BootstrapServiceRegistryBuilder 实例源码

项目:db-bootstrap    文件:HibernateBootstrapProvider.java   
/**
 * Create a {@link Session} based on the provided configuration file.
 *
 * @param prefix - the prefix for using when loading properties for the session
 * @param hibernateCfg - The location of the hibernate configuration file.
 * @param classLoader - class loader to use with the session factory
 * @return {@link Session}
 * @throws Exception
 */
private Session createSession(final String prefix, final String hibernateCfg, final ClassLoader classLoader)
        throws Exception {
    SessionFactory sessionFactory = null;
    DbBootstrapLogger.ROOT_LOGGER.tracef("Using hibernate configuration file %s", hibernateCfg);

    BootstrapServiceRegistryBuilder serviceRegistryBuilder = new BootstrapServiceRegistryBuilder();
    serviceRegistryBuilder.with(classLoader)
            .with(this.getClass().getClassLoader());

    StandardServiceRegistryBuilder standardRegistryBuilder = new StandardServiceRegistryBuilder(serviceRegistryBuilder.build())
            .configure(hibernateCfg);

    configureSettingsFromSystemProperties(prefix, standardRegistryBuilder);
    Metadata metadata = new MetadataSources(standardRegistryBuilder.build() ).buildMetadata();

    sessionFactory = metadata.getSessionFactoryBuilder()
            .build();

    return sessionFactory.openSession();
}
项目:db-bootstrap    文件:HibernateTestUtil.java   
public static SessionFactory getSessionFactory() {
    try {
        if (sessionFactory == null) {
            BootstrapServiceRegistryBuilder serviceRegistryBuilder = new BootstrapServiceRegistryBuilder();
            serviceRegistryBuilder.with(HibernateTestUtil.class.getClassLoader());

            StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder(serviceRegistryBuilder.build())
                    .configure( "META-INF/hibernate.cfg.xml" )
                    .build();
            Metadata metadata = new MetadataSources( standardRegistry ).buildMetadata();

            sessionFactory = metadata.getSessionFactoryBuilder()
                    .build();
        }
    } catch (Throwable ex) {
        throw new ExceptionInInitializerError(ex);
    }
    return sessionFactory;
}
项目:hibernate-types    文件:AbstractTest.java   
private SessionFactory newSessionFactory() {
    final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
            .enableAutoClose();

    Integrator integrator = integrator();
    if (integrator != null) {
        bsrb.applyIntegrator(integrator);
    }

    final BootstrapServiceRegistry bsr = bsrb.build();

    final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bsr)
            .applySettings(properties())
            .build();

    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);

    for (Class annotatedClass : entities()) {
        metadataSources.addAnnotatedClass(annotatedClass);
    }

    String[] packages = packages();
    if (packages != null) {
        for (String annotatedPackage : packages) {
            metadataSources.addPackage(annotatedPackage);
        }
    }

    String[] resources = resources();
    if (resources != null) {
        for (String resource : resources) {
            metadataSources.addResource(resource);
        }
    }

    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
    metadataBuilder.enableNewIdentifierGeneratorSupport(true);
    metadataBuilder.applyImplicitNamingStrategy(ImplicitNamingStrategyLegacyJpaImpl.INSTANCE);

    MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();

    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    Interceptor interceptor = interceptor();
    if (interceptor != null) {
        sfb.applyInterceptor(interceptor);
    }

    return sfb.build();
}
项目:hibernate-types    文件:AbstractTest.java   
private SessionFactory newSessionFactory() {
    final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
            .enableAutoClose();

    Integrator integrator = integrator();
    if (integrator != null) {
        bsrb.applyIntegrator(integrator);
    }

    final BootstrapServiceRegistry bsr = bsrb.build();

    final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bsr)
            .applySettings(properties())
            .build();

    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);

    for (Class annotatedClass : entities()) {
        metadataSources.addAnnotatedClass(annotatedClass);
    }

    String[] packages = packages();
    if (packages != null) {
        for (String annotatedPackage : packages) {
            metadataSources.addPackage(annotatedPackage);
        }
    }

    String[] resources = resources();
    if (resources != null) {
        for (String resource : resources) {
            metadataSources.addResource(resource);
        }
    }

    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
    metadataBuilder.enableNewIdentifierGeneratorSupport(true);
    metadataBuilder.applyImplicitNamingStrategy(ImplicitNamingStrategyLegacyJpaImpl.INSTANCE);

    MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();

    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    Interceptor interceptor = interceptor();
    if (interceptor != null) {
        sfb.applyInterceptor(interceptor);
    }

    return sfb.build();
}
项目:high-performance-java-persistence    文件:AbstractTest.java   
private SessionFactory newSessionFactory() {
    final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
        .enableAutoClose();

    Integrator integrator = integrator();
    if (integrator != null) {
        bsrb.applyIntegrator( integrator );
    }

    final BootstrapServiceRegistry bsr = bsrb.build();

    final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bsr)
        .applySettings(properties())
        .build();

    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);

    for (Class annotatedClass : entities()) {
        metadataSources.addAnnotatedClass(annotatedClass);
    }

    String[] packages = packages();
    if (packages != null) {
        for (String annotatedPackage : packages) {
            metadataSources.addPackage(annotatedPackage);
        }
    }

    String[] resources = resources();
    if (resources != null) {
        for (String resource : resources) {
            metadataSources.addResource(resource);
        }
    }

    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
    metadataBuilder.enableNewIdentifierGeneratorSupport(true);
    metadataBuilder.applyImplicitNamingStrategy(ImplicitNamingStrategyLegacyJpaImpl.INSTANCE);

    final List<Type> additionalTypes = additionalTypes();
    if (additionalTypes != null) {
        additionalTypes.stream().forEach(type -> {
            metadataBuilder.applyTypes((typeContributions, serviceRegistry1) -> {
                if(type instanceof BasicType) {
                    typeContributions.contributeType((BasicType) type);
                } else if (type instanceof UserType ){
                    typeContributions.contributeType((UserType) type);
                } else if (type instanceof CompositeUserType) {
                    typeContributions.contributeType((CompositeUserType) type);
                }
            });
        });
    }

    MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();

    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    Interceptor interceptor = interceptor();
    if(interceptor != null) {
        sfb.applyInterceptor(interceptor);
    }

    return sfb.build();
}
项目:jooby    文件:Hbm.java   
@Override
public void configure(final Env env, final Config conf, final Binder binder) {
  Key<DataSource> dskey = Key.get(DataSource.class, Names.named(name));
  DataSource ds = env.get(dskey)
      .orElseThrow(() -> new NoSuchElementException("DataSource missing: " + dskey));

  BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder();
  bsrb.applyIntegrator(new JpaIntegrator());

  this.bsrb.accept(bsrb, conf);

  String ddl_auto = env.name().equals("dev") ? "update" : "none";

  BootstrapServiceRegistry bsr = bsrb.build();
  StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(bsr);
  ssrb.applySetting(AvailableSettings.HBM2DDL_AUTO, ddl_auto);

  ssrb.applySettings(settings(env, conf));

  this.ssrb.accept(ssrb, conf);

  ssrb.applySetting(AvailableSettings.DATASOURCE, ds);
  ssrb.applySetting(org.hibernate.jpa.AvailableSettings.DELAY_CDI_ACCESS, true);

  StandardServiceRegistry serviceRegistry = ssrb.build();

  MetadataSources sources = new MetadataSources(serviceRegistry);
  this.sources.forEach(src -> src.accept(sources, conf));
  this.metaSources.accept(sources, conf);

  /** scan package? */
  List<URL> packages = sources.getAnnotatedPackages()
      .stream()
      .map(pkg -> getClass().getResource("/" + pkg.replace('.', '/')))
      .collect(Collectors.toList());

  Metadata metadata = sources.getMetadataBuilder()
      .applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
      .applyScanEnvironment(new ScanEnvImpl(packages))
      .build();

  SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
  this.sfb.accept(sfb, conf);
  sfb.applyName(name);

  CompletableFuture<Registry> registry = new CompletableFuture<>();
  sfb.applyBeanManager(GuiceBeanManager.beanManager(registry));

  SessionFactory sessionFactory = sfb.build();
  this.sf.accept(sessionFactory, conf);

  Provider<Session> session = new SessionProvider(sessionFactory);

  ServiceKey serviceKey = env.serviceKey();
  serviceKey.generate(SessionFactory.class, name,
      k -> binder.bind(k).toInstance(sessionFactory));
  serviceKey.generate(EntityManagerFactory.class, name,
      k -> binder.bind(k).toInstance(sessionFactory));

  /** Session/Entity Manager . */
  serviceKey.generate(Session.class, name,
      k -> binder.bind(k).toProvider(session));
  serviceKey.generate(EntityManager.class, name,
      k -> binder.bind(k).toProvider(session));

  /** Unit of work . */
  Provider<UnitOfWork> uow = new UnitOfWorkProvider(sessionFactory);
  serviceKey.generate(UnitOfWork.class, name,
      k -> binder.bind(k).toProvider(uow));

  bindings.forEach(it -> it.accept(binder));

  env.onStart(r -> {
    registry.complete(r);
    listeners.forEach(it -> it.accept((SessionFactoryImplementor) sessionFactory, r));
  });

  env.onStop(sessionFactory::close);
}
项目:hibernate-plus    文件:HibernateSpringSessionFactoryBean.java   
/**
 * Determine the Hibernate {@link MetadataSources} to use.
 * <p>
 * Can also be externally called to initialize and pre-populate a
 * {@link MetadataSources} instance which is then going to be used for
 * {@link SessionFactory} building.
 *
 * @return the MetadataSources to use (never {@code null})
 * @see HibernateSpringSessionFactoryBuilder#HibernateSpringSessionFactoryBuilder(DataSource,
 * ResourceLoader, MetadataSources)
 * @since 4.3
 */
public MetadataSources getMetadataSources() {
    if (this.metadataSources == null) {
        BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder();
        if (this.resourcePatternResolver != null) {
            builder = builder.applyClassLoader(this.resourcePatternResolver.getClassLoader());
        }
        this.metadataSources = new MetadataSources(builder.build());
    }
    return this.metadataSources;
}
项目:gorm-hibernate5    文件:HibernateMappingContextConfiguration.java   
/**
 * Creates the {@link BootstrapServiceRegistryBuilder} to use
 *
 * @return The {@link BootstrapServiceRegistryBuilder}
 */
protected BootstrapServiceRegistryBuilder createBootstrapServiceRegistryBuilder() {
    return new BootstrapServiceRegistryBuilder();
}
项目:hibernate-plus    文件:HibernateSpringSessionFactoryBuilder.java   
/**
 * Create a new LocalSessionFactoryBuilder for the given DataSource.
 *
 * @param dataSource     the JDBC DataSource that the resulting Hibernate SessionFactory should be using
 *                       (may be {@code null})
 * @param resourceLoader the ResourceLoader to load application classes from
 */
public HibernateSpringSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader) {
    this(dataSource, resourceLoader, new MetadataSources(
            new BootstrapServiceRegistryBuilder().applyClassLoader(resourceLoader.getClassLoader()).build()));
}
项目:jooby    文件:Hbm.java   
/**
 * Configurer callback to apply advanced configuration while bootstrapping hibernate:
 *
 * @param configurer Configurer callback.
 * @return This module
 */
public <T> Hbm doWithBootstrap(final BiConsumer<BootstrapServiceRegistryBuilder, Config> configurer) {
  this.bsrb = configurer;
  return this;
}
项目:jooby    文件:Hbm.java   
/**
 * Configurer callback to apply advanced configuration while bootstrapping hibernate:
 *
 * @param configurer Configurer callback.
 * @return This module
 */
public <T> Hbm doWithBootstrap(final Consumer<BootstrapServiceRegistryBuilder> configurer) {
  return doWithBootstrap((builder, conf) -> configurer.accept(builder));
}