Example usage for org.hibernate.boot Metadata buildSessionFactory

List of usage examples for org.hibernate.boot Metadata buildSessionFactory

Introduction

In this page you can find the example usage for org.hibernate.boot Metadata buildSessionFactory.

Prototype

SessionFactory buildSessionFactory();

Source Link

Document

Short-hand form of building a org.hibernate.SessionFactory through the builder without any additional option overrides.

Usage

From source file:com.fitbur.testify.fixture.common.SessionFactoryFactoryBean.java

License:Apache License

@Override
public SessionFactory getObject() throws Exception {
    StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
            .loadProperties("hibernate.properties").applySetting(Environment.DATASOURCE, dataSource).build();

    Reflections reflections = new Reflections(DatabaseConfig.class.getPackage().getName());
    MetadataSources metadataSources = new MetadataSources(registry);

    MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder()
            .applyPhysicalNamingStrategy(new PhysicalNamingStrategyStandardImpl())
            .applyImplicitNamingStrategy(new ImplicitNamingStrategyComponentPathImpl());

    reflections.getSubTypesOf(AttributeConverter.class).parallelStream()
            .forEach(metadataBuilder::applyAttributeConverter);

    reflections.getTypesAnnotatedWith(Entity.class).parallelStream()
            .forEach(metadataSources::addAnnotatedClass);

    Metadata metadata = metadataBuilder.build();

    return metadata.buildSessionFactory();
}

From source file:com.fitbur.testify.fixture.PostgresDockerConfig.java

License:Apache License

@Bean
FactoryBean<SessionFactory> sessionFactoryImplProvider(DataSource dataSource) {
    return new SmartFactoryBean<SessionFactory>() {
        @Override/*from  ww w  . j  a va  2s .  c om*/
        public boolean isPrototype() {
            return false;
        }

        @Override
        public boolean isEagerInit() {
            return true;
        }

        @Override
        public SessionFactory getObject() throws Exception {
            StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                    .loadProperties("hibernate.properties").applySetting(Environment.DATASOURCE, dataSource)
                    .build();

            Reflections reflections = new Reflections(PostgresDockerConfig.class.getPackage().getName());
            MetadataSources metadataSources = new MetadataSources(registry);

            MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder()
                    .applyPhysicalNamingStrategy(new PhysicalNamingStrategyStandardImpl())
                    .applyImplicitNamingStrategy(new ImplicitNamingStrategyComponentPathImpl());

            reflections.getSubTypesOf(AttributeConverter.class).parallelStream()
                    .forEach(metadataBuilder::applyAttributeConverter);

            reflections.getTypesAnnotatedWith(Entity.class).parallelStream()
                    .forEach(metadataSources::addAnnotatedClass);

            Metadata metadata = metadataBuilder.build();

            return metadata.buildSessionFactory();
        }

        @Override
        public Class<?> getObjectType() {
            return SessionFactory.class;
        }

        @Override
        public boolean isSingleton() {
            return true;
        }
    };

}

From source file:org.apache.ignite.cache.hibernate.HibernateL2CacheStrategySelfTest.java

License:Apache License

/**
 * @param accessType Cache access typr.//from   w  ww .  j  a v a2 s. com
 * @param igniteInstanceName Name of the grid providing caches.
 * @return Session factory.
 */
private SessionFactory startHibernate(AccessType accessType, String igniteInstanceName) {
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();

    builder.applySetting("hibernate.connection.url", CONNECTION_URL);

    for (Map.Entry<String, String> e : HibernateL2CacheSelfTest
            .hibernateProperties(igniteInstanceName, accessType.name()).entrySet())
        builder.applySetting(e.getKey(), e.getValue());

    builder.applySetting(USE_STRUCTURED_CACHE, "true");
    builder.applySetting(REGION_CACHE_PROPERTY + ENTITY1_NAME, "cache1");
    builder.applySetting(REGION_CACHE_PROPERTY + ENTITY2_NAME, "cache2");
    builder.applySetting(REGION_CACHE_PROPERTY + TIMESTAMP_CACHE, TIMESTAMP_CACHE);
    builder.applySetting(REGION_CACHE_PROPERTY + QUERY_CACHE, QUERY_CACHE);

    MetadataSources metadataSources = new MetadataSources(builder.build());

    metadataSources.addAnnotatedClass(Entity1.class);
    metadataSources.addAnnotatedClass(Entity2.class);
    metadataSources.addAnnotatedClass(Entity3.class);
    metadataSources.addAnnotatedClass(Entity4.class);

    Metadata metadata = metadataSources.buildMetadata();

    for (PersistentClass entityBinding : metadata.getEntityBindings()) {
        if (!entityBinding.isInherited())
            ((RootClass) entityBinding).setCacheConcurrencyStrategy(accessType.getExternalName());
    }

    return metadata.buildSessionFactory();
}

From source file:org.infinispan.test.hibernate.cache.commons.stress.CorrectnessTestCase.java

License:LGPL

@BeforeClassOnce
public void beforeClass() {
    TestResourceTracker.testStarted(getClass().getSimpleName());
    Arrays.asList(new File(System.getProperty("java.io.tmpdir"))
            .listFiles((dir, name) -> name.startsWith("family_") || name.startsWith("invalidations-"))).stream()
            .forEach(f -> f.delete());
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().enableAutoClose()
            .applySetting(Environment.USE_SECOND_LEVEL_CACHE, "true")
            .applySetting(Environment.USE_QUERY_CACHE, "true").applySetting(Environment.DRIVER, "org.h2.Driver")
            .applySetting(Environment.URL, "jdbc:h2:mem:" + getDbName() + ";TRACE_LEVEL_FILE=4")
            .applySetting(Environment.DIALECT, H2Dialect.class.getName())
            .applySetting(Environment.HBM2DDL_AUTO, "create-drop")
            .applySetting(TestRegionFactory.CONFIGURATION_HOOK, InjectFailures.class)
            .applySetting(TestRegionFactory.CACHE_MODE, cacheMode)
            .applySetting(Environment.USE_MINIMAL_PUTS, "false")
            .applySetting(Environment.GENERATE_STATISTICS, "false");
    applySettings(ssrb);//from   ww w  .  ja v  a2  s.  c  o m

    sessionFactories = new SessionFactory[NUM_NODES];
    for (int i = 0; i < NUM_NODES; ++i) {
        StandardServiceRegistry registry = ssrb.build();
        Metadata metadata = buildMetadata(registry);
        sessionFactories[i] = metadata.buildSessionFactory();
    }
}

From source file:org.infinispan.test.hibernate.cache.commons.stress.PutFromLoadStressTestCase.java

License:LGPL

@BeforeClass
public static void beforeClass() {
    // Extra options located in src/test/resources/hibernate.properties
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
            .applySetting(Environment.USE_SECOND_LEVEL_CACHE, "true")
            .applySetting(Environment.USE_QUERY_CACHE, "true")
            // TODO: Tweak to have a fully local region factory (no transport, cache mode = local, no marshalling, ...etc)
            .applySetting(Environment.CACHE_REGION_FACTORY,
                    "org.infinispan.hibernate.cache.InfinispanRegionFactory")
            .applySetting(Environment.JTA_PLATFORM, new NarayanaStandaloneJtaPlatform())
            // Force minimal puts off to simplify stressing putFromLoad logic
            .applySetting(Environment.USE_MINIMAL_PUTS, "false")
            .applySetting(Environment.HBM2DDL_AUTO, "create-drop");

    StandardServiceRegistry serviceRegistry = ssrb.build();

    MetadataSources metadataSources = new MetadataSources(serviceRegistry)
            .addResource("cache/infinispan/functional/Item.hbm.xml")
            .addResource("cache/infinispan/functional/Customer.hbm.xml")
            .addResource("cache/infinispan/functional/Contact.hbm.xml").addAnnotatedClass(Age.class);

    Metadata metadata = metadataSources.buildMetadata();
    for (PersistentClass entityBinding : metadata.getEntityBindings()) {
        if (entityBinding instanceof RootClass) {
            ((RootClass) entityBinding).setCacheConcurrencyStrategy("transactional");
        }//from w w  w .  ja  v  a2s .c o  m
    }
    for (Collection collectionBinding : metadata.getCollectionBindings()) {
        collectionBinding.setCacheConcurrencyStrategy("transactional");
    }

    sessionFactory = metadata.buildSessionFactory();
    tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
}

From source file:org.infinispan.test.hibernate.cache.commons.stress.SecondLevelCacheStressTestCase.java

License:LGPL

@Before
public void beforeClass() {
    provider = getProvider();//w ww  .j av a2 s .c  o  m

    updatedIds = ConcurrentHashMap.newKeySet();
    removeIds = new ConcurrentLinkedQueue<Integer>();

    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().enableAutoClose()
            .applySetting(Environment.USE_SECOND_LEVEL_CACHE, "true")
            .applySetting(Environment.USE_QUERY_CACHE, "true")
            .applySetting(Environment.DRIVER, "com.mysql.jdbc.Driver")
            .applySetting(Environment.URL, "jdbc:mysql://localhost:3306/hibernate")
            .applySetting(Environment.DIALECT, "org.hibernate.dialect.MySQL5InnoDBDialect")
            .applySetting(Environment.USER, "root").applySetting(Environment.PASS, "password")
            .applySetting(Environment.HBM2DDL_AUTO, "create-drop");

    // Create database schema in each run
    applyCacheSettings(ssrb);

    StandardServiceRegistry registry = ssrb.build();

    Metadata metadata = buildMetadata(registry);

    sessionFactory = metadata.buildSessionFactory();

    tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
}

From source file:org.infinispan.test.hibernate.cache.commons.tm.JBossStandaloneJtaExampleTest.java

License:LGPL

private SessionFactory buildSessionFactory() {
    // Extra options located in src/test/resources/hibernate.properties
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
            .applySetting(Environment.DIALECT, "HSQL").applySetting(Environment.HBM2DDL_AUTO, "create-drop")
            .applySetting(Environment.CONNECTION_PROVIDER, JtaAwareConnectionProviderImpl.class.getName())
            .applySetting(Environment.JNDI_CLASS, "org.jnp.interfaces.NamingContextFactory")
            .applySetting(Environment.TRANSACTION_COORDINATOR_STRATEGY,
                    JtaTransactionCoordinatorBuilderImpl.class.getName())
            .applySetting(Environment.CURRENT_SESSION_CONTEXT_CLASS, "jta")
            .applySetting(Environment.RELEASE_CONNECTIONS, "auto")
            .applySetting(Environment.USE_SECOND_LEVEL_CACHE, "true")
            .applySetting(Environment.USE_QUERY_CACHE, "true")
            .applySetting(Environment.JTA_PLATFORM, new NarayanaStandaloneJtaPlatform())
            .applySetting(Environment.CACHE_REGION_FACTORY,
                    TestRegionFactoryProvider.load().getRegionFactoryClass().getName());

    StandardServiceRegistry serviceRegistry = ssrb.build();

    MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    metadataSources.addResource("org/infinispan/test/hibernate/cache/commons/functional/entities/Item.hbm.xml");

    Metadata metadata = metadataSources.buildMetadata();
    for (PersistentClass entityBinding : metadata.getEntityBindings()) {
        if (entityBinding instanceof RootClass) {
            RootClass rootClass = (RootClass) entityBinding;
            rootClass.setCacheConcurrencyStrategy("transactional");
            rootClass.setCachingExplicitlyRequested(true);
        }//from  w ww.  ja v  a  2  s .c o  m
    }
    for (Collection collectionBinding : metadata.getCollectionBindings()) {
        collectionBinding.setCacheConcurrencyStrategy("transactional");
    }

    return metadata.buildSessionFactory();
}

From source file:org.infinispan.test.hibernate.cache.stress.CorrectnessTestCase.java

License:LGPL

@BeforeClassOnce
public void beforeClass() {
    TestResourceTracker.testStarted(getClass().getSimpleName());
    Arrays.asList(new File(System.getProperty("java.io.tmpdir"))
            .listFiles((dir, name) -> name.startsWith("family_") || name.startsWith("invalidations-"))).stream()
            .forEach(f -> f.delete());
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().enableAutoClose()
            .applySetting(Environment.USE_SECOND_LEVEL_CACHE, "true")
            .applySetting(Environment.USE_QUERY_CACHE, "true").applySetting(Environment.DRIVER, "org.h2.Driver")
            .applySetting(Environment.URL, "jdbc:h2:mem:" + getDbName() + ";TRACE_LEVEL_FILE=4")
            .applySetting(Environment.DIALECT, H2Dialect.class.getName())
            .applySetting(Environment.HBM2DDL_AUTO, "create-drop")
            .applySetting(Environment.CACHE_REGION_FACTORY, FailingInfinispanRegionFactory.class.getName())
            .applySetting(TestInfinispanRegionFactory.CACHE_MODE, cacheMode)
            .applySetting(Environment.USE_MINIMAL_PUTS, "false")
            .applySetting(Environment.GENERATE_STATISTICS, "false");
    applySettings(ssrb);//from   w  ww  .j  a  v a  2  s  .  co  m

    sessionFactories = new SessionFactory[NUM_NODES];
    for (int i = 0; i < NUM_NODES; ++i) {
        StandardServiceRegistry registry = ssrb.build();
        Metadata metadata = buildMetadata(registry);
        sessionFactories[i] = metadata.buildSessionFactory();
    }
}

From source file:org.infinispan.test.hibernate.cache.stress.PutFromLoadStressTestCase.java

License:LGPL

@BeforeClass
public static void beforeClass() {
    // Extra options located in src/test/resources/hibernate.properties
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
            .applySetting(Environment.USE_SECOND_LEVEL_CACHE, "true")
            .applySetting(Environment.USE_QUERY_CACHE, "true")
            // TODO: Tweak to have a fully local region factory (no transport, cache mode = local, no marshalling, ...etc)
            .applySetting(Environment.CACHE_REGION_FACTORY,
                    "org.infinispan.hibernate.cache.InfinispanRegionFactory")
            .applySetting(Environment.JTA_PLATFORM,
                    "org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform")
            // Force minimal puts off to simplify stressing putFromLoad logic
            .applySetting(Environment.USE_MINIMAL_PUTS, "false")
            .applySetting(Environment.HBM2DDL_AUTO, "create-drop");

    StandardServiceRegistry serviceRegistry = ssrb.build();

    MetadataSources metadataSources = new MetadataSources(serviceRegistry)
            .addResource("cache/infinispan/functional/Item.hbm.xml")
            .addResource("cache/infinispan/functional/Customer.hbm.xml")
            .addResource("cache/infinispan/functional/Contact.hbm.xml").addAnnotatedClass(Age.class);

    Metadata metadata = metadataSources.buildMetadata();
    for (PersistentClass entityBinding : metadata.getEntityBindings()) {
        if (entityBinding instanceof RootClass) {
            ((RootClass) entityBinding).setCacheConcurrencyStrategy("transactional");
        }//from  w  w  w  .j  av  a  2s.c  o m
    }
    for (Collection collectionBinding : metadata.getCollectionBindings()) {
        collectionBinding.setCacheConcurrencyStrategy("transactional");
    }

    sessionFactory = metadata.buildSessionFactory();
    tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
}

From source file:org.infinispan.test.hibernate.cache.stress.SecondLevelCacheStressTestCase.java

License:LGPL

@Before
public void beforeClass() {
    provider = getProvider();/*  w  ww .  jav  a 2  s. c  om*/

    updatedIds = new ConcurrentHashSet<Integer>();
    removeIds = new ConcurrentLinkedQueue<Integer>();

    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().enableAutoClose()
            .applySetting(Environment.USE_SECOND_LEVEL_CACHE, "true")
            .applySetting(Environment.USE_QUERY_CACHE, "true")
            .applySetting(Environment.DRIVER, "com.mysql.jdbc.Driver")
            .applySetting(Environment.URL, "jdbc:mysql://localhost:3306/hibernate")
            .applySetting(Environment.DIALECT, "org.hibernate.dialect.MySQL5InnoDBDialect")
            .applySetting(Environment.USER, "root").applySetting(Environment.PASS, "password")
            .applySetting(Environment.HBM2DDL_AUTO, "create-drop");

    // Create database schema in each run
    applyCacheSettings(ssrb);

    StandardServiceRegistry registry = ssrb.build();

    Metadata metadata = buildMetadata(registry);

    sessionFactory = metadata.buildSessionFactory();

    tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
}