Example usage for org.hibernate.boot.registry StandardServiceRegistryBuilder StandardServiceRegistryBuilder

List of usage examples for org.hibernate.boot.registry StandardServiceRegistryBuilder StandardServiceRegistryBuilder

Introduction

In this page you can find the example usage for org.hibernate.boot.registry StandardServiceRegistryBuilder StandardServiceRegistryBuilder.

Prototype

public StandardServiceRegistryBuilder(BootstrapServiceRegistry bootstrapServiceRegistry) 

Source Link

Document

Create a builder with the specified bootstrap services.

Usage

From source file:com.foilen.smalltools.tools.Hibernate50Tools.java

License:Open Source License

/**
 * Generate the SQL file. This is based on the code in {@link LocalSessionFactoryBuilder#scanPackages(String...)}
 *
 * @param dialect/*from  w w w.  j a v a  2  s  .  co  m*/
 *            the dialect (e.g: org.hibernate.dialect.MySQL5InnoDBDialect )
 * @param outputSqlFile
 *            where to put the generated SQL file
 * @param useUnderscore
 *            true: to have tables names like "employe_manager" ; false: to have tables names like "employeManager"
 * @param packagesToScan
 *            the packages where your entities are
 */
public static void generateSqlSchema(Class<? extends Dialect> dialect, String outputSqlFile,
        boolean useUnderscore, String... packagesToScan) {

    BootstrapServiceRegistry bootstrapServiceRegistry = new BootstrapServiceRegistryBuilder().build();

    MetadataSources metadataSources = new MetadataSources(bootstrapServiceRegistry);

    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
            false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
    scanner.addIncludeFilter(new AnnotationTypeFilter(Embeddable.class));
    scanner.addIncludeFilter(new AnnotationTypeFilter(MappedSuperclass.class));
    for (String pkg : packagesToScan) {
        for (BeanDefinition beanDefinition : scanner.findCandidateComponents(pkg)) {
            metadataSources.addAnnotatedClassName(beanDefinition.getBeanClassName());
        }
    }

    StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder(
            bootstrapServiceRegistry);
    standardServiceRegistryBuilder.applySetting(AvailableSettings.DIALECT, dialect.getName());
    StandardServiceRegistryImpl ssr = (StandardServiceRegistryImpl) standardServiceRegistryBuilder.build();
    MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder(ssr);

    if (useUnderscore) {
        metadataBuilder.applyImplicitNamingStrategy(new SpringImplicitNamingStrategy());
        metadataBuilder.applyPhysicalNamingStrategy(new SpringPhysicalNamingStrategy());
    }

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

    // Exportation
    SchemaExport schemaExport = new SchemaExport(metadata);
    schemaExport.setOutputFile(outputSqlFile);
    schemaExport.setDelimiter(";");
    schemaExport.setFormat(true);
    schemaExport.execute(true, false, false, true);
}

From source file:com.foilen.smalltools.tools.Hibernate51Tools.java

License:Open Source License

/**
 * Generate the SQL file. This is based on the code in {@link LocalSessionFactoryBuilder#scanPackages(String...)}
 *
 * @param dialect/*  w w w  . j  av a2s  . com*/
 *            the dialect (e.g: org.hibernate.dialect.MySQL5InnoDBDialect )
 * @param outputSqlFile
 *            where to put the generated SQL file
 * @param useUnderscore
 *            true: to have tables names like "employe_manager" ; false: to have tables names like "employeManager"
 * @param packagesToScan
 *            the packages where your entities are
 */
public static void generateSqlSchema(Class<? extends Dialect> dialect, String outputSqlFile,
        boolean useUnderscore, String... packagesToScan) {

    BootstrapServiceRegistry bootstrapServiceRegistry = new BootstrapServiceRegistryBuilder().build();

    MetadataSources metadataSources = new MetadataSources(bootstrapServiceRegistry);

    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
            false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
    scanner.addIncludeFilter(new AnnotationTypeFilter(Embeddable.class));
    scanner.addIncludeFilter(new AnnotationTypeFilter(MappedSuperclass.class));
    for (String pkg : packagesToScan) {
        for (BeanDefinition beanDefinition : scanner.findCandidateComponents(pkg)) {
            metadataSources.addAnnotatedClassName(beanDefinition.getBeanClassName());
        }
    }

    StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder(
            bootstrapServiceRegistry);
    standardServiceRegistryBuilder.applySetting(AvailableSettings.DIALECT, dialect.getName());
    StandardServiceRegistryImpl ssr = (StandardServiceRegistryImpl) standardServiceRegistryBuilder.build();
    MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder(ssr);

    if (useUnderscore) {
        metadataBuilder.applyImplicitNamingStrategy(new SpringImplicitNamingStrategy());
        metadataBuilder.applyPhysicalNamingStrategy(new SpringPhysicalNamingStrategy());
    }

    new SchemaExport() //
            .setHaltOnError(true) //
            .setOutputFile(outputSqlFile) //
            .setFormat(true) //
            .setDelimiter(";") //
            .execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, metadataBuilder.build());

}

From source file:com.zeroone.guestebook.domain.SchemaGenerator.java

License:Apache License

public SchemaGenerator(String packageName, Dialect dialect) throws Exception {
    BootstrapServiceRegistry bsr;//from w  w w. ja  v  a2 s  .  co  m
    bsr = new BootstrapServiceRegistryBuilder().build();
    StandardServiceRegistryBuilder ssrBuilder;
    ssrBuilder = new StandardServiceRegistryBuilder(bsr);
    ssrBuilder.applySetting("hibernate.dialect", dialect.dialectClass);
    ServiceRegistry serviceRegistry = ssrBuilder.build();
    MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    for (Class<?> clazz : getClasses(packageName)) {
        metadataSources.addAnnotatedClass(clazz);
    }
    MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
    this.metadata = (MetadataImplementor) metadataBuilder.build();
    this.dialect = dialect;
}

From source file:org.babyfish.hibernate.internal.XMetadataBuilderImpl.java

License:Open Source License

private static StandardServiceRegistry getStandardServiceRegistry(ServiceRegistry serviceRegistry) {
    if (serviceRegistry == null) {
        throw new HibernateException("ServiceRegistry passed to MetadataBuilder cannot be null");
    }/*from   w  ww .  j  a  v a2  s.  com*/

    if (StandardServiceRegistry.class.isInstance(serviceRegistry)) {
        return (StandardServiceRegistry) serviceRegistry;
    } else if (BootstrapServiceRegistry.class.isInstance(serviceRegistry)) {
        LOGGER.debug(
                "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()));
    }
}

From source file:org.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration.java

License:Apache License

@Override
public SessionFactory buildSessionFactory() throws HibernateException {

    // set the class loader to load Groovy classes

    // work around for HHH-2624
    Map<String, Type> empty = new HashMap<String, Type>();
    addFilterDefinition(new FilterDefinition("dynamicFilterEnabler", "1=1", empty));

    SessionFactory sessionFactory = null;

    ClassLoader appClassLoader = (ClassLoader) getProperties().get(AvailableSettings.APP_CLASSLOADER);
    Thread currentThread = Thread.currentThread();
    ClassLoader threadContextClassLoader = currentThread.getContextClassLoader();
    boolean overrideClassLoader = (appClassLoader != null && !appClassLoader.equals(threadContextClassLoader));
    if (overrideClassLoader) {
        currentThread.setContextClassLoader(appClassLoader);
    }/*from   w w  w  . java2  s  .  com*/

    try {
        ConfigurationHelper.resolvePlaceHolders(getProperties());

        EventListenerIntegrator eventListenerIntegrator = new EventListenerIntegrator(hibernateEventListeners,
                eventListeners);
        BootstrapServiceRegistry bootstrapServiceRegistry = new BootstrapServiceRegistryBuilder()
                .with(eventListenerIntegrator).build();

        setSessionFactoryObserver(new SessionFactoryObserver() {
            private static final long serialVersionUID = 1;

            public void sessionFactoryCreated(SessionFactory factory) {
            }

            public void sessionFactoryClosed(SessionFactory factory) {
                ((ServiceRegistryImplementor) serviceRegistry).destroy();
            }
        });

        StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder(
                bootstrapServiceRegistry).applySettings(getProperties());
        sessionFactory = super.buildSessionFactory(standardServiceRegistryBuilder.build());
        serviceRegistry = ((SessionFactoryImplementor) sessionFactory).getServiceRegistry();
    } finally {
        if (overrideClassLoader) {
            currentThread.setContextClassLoader(threadContextClassLoader);
        }
    }

    return sessionFactory;
}

From source file:org.granite.test.tide.hibernate4.data.TestHibernate4ChangeSetMerge.java

License:Open Source License

@Override
protected void initPersistence() {
    Configuration configuration = new Configuration().addAnnotatedClass(AbstractEntity.class)
            .addAnnotatedClass(Address.class).addAnnotatedClass(Contact1.class).addAnnotatedClass(Country.class)
            .addAnnotatedClass(Person1.class).addAnnotatedClass(Phone.class).addAnnotatedClass(Contact2.class)
            .addAnnotatedClass(Person2.class).addAnnotatedClass(Phone2.class)
            .setProperty("hibernate.dialect", org.hibernate.dialect.H2Dialect.class.getName())
            .setProperty("hibernate.hbm2ddl.auto", "create-drop").setProperty("hibernate.show_sql", "true")
            .setProperty("hibernate.connection.driver_class", org.h2.Driver.class.getName())
            .setProperty("hibernate.connection.url", "jdbc:h2:mem:test-loader")
            .setProperty("hibernate.connection.username", "sa")
            .setProperty("hibernate.connection.password", "");

    BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
            .with(new Hibernate4ChangeSetIntegrator());
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(bsrb.build())
            .applySettings(configuration.getProperties());
    ServiceRegistry serviceRegistry = ssrb.build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}

From source file:org.granite.test.tide.hibernate4.data.TestHibernate4ChangeSetPublisher.java

License:Open Source License

protected void initPersistence() {
    Configuration configuration = new Configuration().addAnnotatedClass(AbstractEntity.class)
            .addAnnotatedClass(LegalEntity.class).addAnnotatedClass(Address.class)
            .addAnnotatedClass(Contact1.class).addAnnotatedClass(Country.class).addAnnotatedClass(Person1.class)
            .addAnnotatedClass(Person4.class).addAnnotatedClass(Contact4.class).addAnnotatedClass(Phone.class)
            .addAnnotatedClass(Phone2.class).addAnnotatedClass(Phone4.class).addAnnotatedClass(OrderRepo.class)
            .addAnnotatedClass(Order.class).addAnnotatedClass(LineItemList.class)
            .addAnnotatedClass(LineItemBag.class).addAnnotatedClass(Order2.class)
            .addAnnotatedClass(LineItemList2.class).addAnnotatedClass(LineItemBag2.class)
            .addAnnotatedClass(Classification.class)
            .setProperty("hibernate.dialect", org.hibernate.dialect.H2Dialect.class.getName())
            .setProperty("hibernate.hbm2ddl.auto", "create-drop").setProperty("hibernate.show_sql", "true")
            .setProperty("hibernate.connection.driver_class", org.h2.Driver.class.getName())
            .setProperty("hibernate.connection.url", "jdbc:h2:mem:test-changeset")
            .setProperty("hibernate.connection.username", "sa")
            .setProperty("hibernate.connection.password", "");

    BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
            .with(new Hibernate4ChangeSetIntegrator());
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(bsrb.build())
            .applySettings(configuration.getProperties());
    ServiceRegistry serviceRegistry = ssrb.build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}

From source file:org.granite.test.tide.hibernate4.data.TestHibernate4DataPublish.java

License:Open Source License

protected void initPersistence() throws Exception {
    Configuration configuration = new Configuration().addAnnotatedClass(AbstractEntity0.class)
            .addAnnotatedClass(Order3.class).addAnnotatedClass(LineItem.class).addAnnotatedClass(Contact5.class)
            .addAnnotatedClass(Location5.class).addAnnotatedClass(Alias5.class)
            .setProperty("hibernate.dialect", org.hibernate.dialect.H2Dialect.class.getName())
            .setProperty("hibernate.hbm2ddl.auto", "create-drop").setProperty("hibernate.show_sql", "true")
            .setProperty("hibernate.connection.driver_class", "org.h2.Driver")
            .setProperty("hibernate.connection.url", "jdbc:h2:mem:test-publish")
            .setProperty("hibernate.connection.username", "sa")
            .setProperty("hibernate.connection.password", "");

    BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
            .with(new Hibernate4Integrator());
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(bsrb.build())
            .applySettings(configuration.getProperties());
    ServiceRegistry serviceRegistry = ssrb.build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}

From source file:org.openflexo.connie.hbn.HbnConfig.java

License:Open Source License

/**
 * Default Constructor/*  w w w .  j  a v  a  2 s .  c  o m*/
 * 
 * @param serviceRegistry
 */
public HbnConfig(BootstrapServiceRegistry serviceRegistry) {
    hbnBsRegistry = serviceRegistry;
    metadataSrc = new MetadataSources(serviceRegistry);
    properties = new Properties();

    hbnRegistryBuilder = new StandardServiceRegistryBuilder(hbnBsRegistry);
}

From source file:org.teiid.spring.autoconfigure.SchemaBuilderUtility.java

License:Apache License

public void generateVBLSchema(ApplicationContext context, MetadataFactory source, MetadataFactory target,
        Dialect dialect, MetadataSources metadataSources) {
    generateVBLSchema(source, target);//from  w  w w  . j a va 2s. c om
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(
            (BootstrapServiceRegistry) metadataSources.getServiceRegistry())
                    .applySetting(AvailableSettings.DIALECT, dialect).build();
    ArtifactCollector files = generateHibernateModel(source, serviceRegistry);
    for (File f : files.getFiles("hbm.xml")) {
        metadataSources.addFile(f);
    }
}