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();
}
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();
}
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();
}
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();
}
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);
}
}
Aggregations