Search in sources :

Example 6 with Configuration

use of org.hibernate.cfg.Configuration in project hibernate-orm by hibernate.

the class CacheKeySerializationTest method getSessionFactory.

private SessionFactoryImplementor getSessionFactory(String cacheKeysFactory) {
    Configuration configuration = new Configuration().setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true").setProperty(Environment.CACHE_REGION_FACTORY, TestInfinispanRegionFactory.class.getName()).setProperty(Environment.DEFAULT_CACHE_CONCURRENCY_STRATEGY, "transactional").setProperty(AvailableSettings.SHARED_CACHE_MODE, "ALL").setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    if (cacheKeysFactory != null) {
        configuration.setProperty(Environment.CACHE_KEYS_FACTORY, cacheKeysFactory);
    }
    configuration.addAnnotatedClass(WithSimpleId.class);
    configuration.addAnnotatedClass(WithEmbeddedId.class);
    return (SessionFactoryImplementor) configuration.buildSessionFactory();
}
Also used : Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor)

Example 7 with Configuration

use of org.hibernate.cfg.Configuration in project hibernate-orm by hibernate.

the class CacheKeysFactoryTest method getSessionFactory.

private SessionFactory getSessionFactory(String cacheKeysFactory) {
    Configuration configuration = new Configuration().setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true").setProperty(Environment.CACHE_REGION_FACTORY, TestInfinispanRegionFactory.class.getName()).setProperty(Environment.DEFAULT_CACHE_CONCURRENCY_STRATEGY, "transactional").setProperty(Environment.JPA_SHARED_CACHE_MODE, "ALL").setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    if (cacheKeysFactory != null) {
        configuration.setProperty(Environment.CACHE_KEYS_FACTORY, cacheKeysFactory);
    }
    configuration.addAnnotatedClass(Person.class);
    return configuration.buildSessionFactory();
}
Also used : Configuration(org.hibernate.cfg.Configuration)

Example 8 with Configuration

use of org.hibernate.cfg.Configuration in project hibernate-orm by hibernate.

the class TypeFactorySerializationTest method testUnregisterSerializeRegisterDiffSessionFactory.

@Test
public void testUnregisterSerializeRegisterDiffSessionFactory() throws Exception {
    Configuration cfg = new Configuration().setProperty(AvailableSettings.SESSION_FACTORY_NAME, NAME).setProperty(AvailableSettings.SESSION_FACTORY_NAME_IS_JNDI, // default is true
    "false");
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory();
    assertSame(factory, SessionFactoryRegistry.INSTANCE.getNamedSessionFactory(NAME));
    // Remove the session factory from the registry
    SessionFactoryRegistry.INSTANCE.removeSessionFactory(factory.getUuid(), NAME, false, null);
    assertNull(SessionFactoryRegistry.INSTANCE.findSessionFactory(factory.getUuid(), NAME));
    TypeFactory typeFactory = factory.getTypeResolver().getTypeFactory();
    byte[] typeFactoryBytes = SerializationHelper.serialize(typeFactory);
    typeFactory = (TypeFactory) SerializationHelper.deserialize(typeFactoryBytes);
    try {
        typeFactory.resolveSessionFactory();
        fail("should have failed with HibernateException because session factory is not registered.");
    } catch (HibernateException ex) {
    // expected because the session factory is not registered.
    }
    // Now create a new session factory with the same name; it will have a different UUID.
    SessionFactoryImplementor factoryWithSameName = (SessionFactoryImplementor) cfg.buildSessionFactory();
    assertSame(factoryWithSameName, SessionFactoryRegistry.INSTANCE.getNamedSessionFactory(NAME));
    assertFalse(factory.getUuid().equals(factoryWithSameName.getUuid()));
    // Session factory resolved from typeFactory should be the new session factory
    // (because it is resolved from SessionFactoryRegistry.INSTANCE)
    assertSame(factoryWithSameName, typeFactory.resolveSessionFactory());
    factory.close();
    factoryWithSameName.close();
}
Also used : Configuration(org.hibernate.cfg.Configuration) HibernateException(org.hibernate.HibernateException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) TypeFactory(org.hibernate.type.TypeFactory) Test(org.junit.Test)

Example 9 with Configuration

use of org.hibernate.cfg.Configuration in project hibernate-orm by hibernate.

the class TypeFactorySerializationTest method testUnregisterSerializeRegisterDiffSessionFactoryNoName.

@Test
public void testUnregisterSerializeRegisterDiffSessionFactoryNoName() throws Exception {
    Configuration cfg = new Configuration();
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory();
    assertSame(factory, SessionFactoryRegistry.INSTANCE.getSessionFactory(factory.getUuid()));
    // Remove the session factory from the registry
    SessionFactoryRegistry.INSTANCE.removeSessionFactory(factory.getUuid(), null, false, null);
    assertNull(SessionFactoryRegistry.INSTANCE.getSessionFactory(factory.getUuid()));
    TypeFactory typeFactory = factory.getTypeResolver().getTypeFactory();
    byte[] typeFactoryBytes = SerializationHelper.serialize(typeFactory);
    typeFactory = (TypeFactory) SerializationHelper.deserialize(typeFactoryBytes);
    try {
        typeFactory.resolveSessionFactory();
        fail("should have failed with HibernateException because session factory is not registered.");
    } catch (HibernateException ex) {
    // expected because the session factory is not registered.
    }
    // Now create a new session factory with the same name; it will have a different UUID.
    SessionFactoryImplementor factoryWithDiffUuid = (SessionFactoryImplementor) cfg.buildSessionFactory();
    assertSame(factoryWithDiffUuid, SessionFactoryRegistry.INSTANCE.getSessionFactory(factoryWithDiffUuid.getUuid()));
    assertFalse(factory.getUuid().equals(factoryWithDiffUuid.getUuid()));
    // It should not be possible to resolve the session factory with no name configured.
    try {
        typeFactory.resolveSessionFactory();
        fail("should have failed with HibernateException because session factories were not registered with the same non-null name.");
    } catch (HibernateException ex) {
    // expected
    }
    factory.close();
    factoryWithDiffUuid.close();
}
Also used : Configuration(org.hibernate.cfg.Configuration) HibernateException(org.hibernate.HibernateException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) TypeFactory(org.hibernate.type.TypeFactory) Test(org.junit.Test)

Example 10 with Configuration

use of org.hibernate.cfg.Configuration in project hibernate-orm by hibernate.

the class AuditedDynamicComponentTest method testAuditedDynamicComponentFailure.

//@Test
public void testAuditedDynamicComponentFailure() throws URISyntaxException {
    final Configuration config = new Configuration();
    final URL hbm = Thread.currentThread().getContextClassLoader().getResource("mappings/dynamicComponents/mapAudited.hbm.xml");
    config.addFile(new File(hbm.toURI()));
    final String auditStrategy = getAuditStrategy();
    if (!StringTools.isEmpty(auditStrategy)) {
        config.setProperty(EnversSettings.AUDIT_STRATEGY, auditStrategy);
    }
    final ServiceRegistry serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(config.getProperties());
    try {
        config.buildSessionFactory(serviceRegistry);
        Assert.fail("MappingException expected");
    } catch (MappingException e) {
        Assert.assertEquals("Audited dynamic-component properties are not supported. Consider applying @NotAudited annotation to " + AuditedDynamicComponentEntity.class.getName() + "#customFields.", e.getMessage());
    } finally {
        ServiceRegistryBuilder.destroy(serviceRegistry);
    }
}
Also used : Configuration(org.hibernate.cfg.Configuration) ServiceRegistry(org.hibernate.service.ServiceRegistry) File(java.io.File) URL(java.net.URL) MappingException(org.hibernate.MappingException)

Aggregations

Configuration (org.hibernate.cfg.Configuration)179 Test (org.junit.Test)66 Session (org.hibernate.Session)37 SessionFactory (org.hibernate.SessionFactory)34 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)27 BeforeClass (org.junit.BeforeClass)19 Before (org.junit.Before)18 ServiceRegistry (org.hibernate.service.ServiceRegistry)14 File (java.io.File)13 Properties (java.util.Properties)11 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)9 EncapsulatedCompositeIdResultSetProcessorTest (org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)8 HibernateException (org.hibernate.HibernateException)7 MappingException (org.hibernate.MappingException)7 Transaction (org.hibernate.Transaction)7 URL (java.net.URL)6 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)6 TestForIssue (org.hibernate.testing.TestForIssue)6 EntityTuplizer (org.hibernate.tuple.entity.EntityTuplizer)6 BigDecimal (java.math.BigDecimal)5