Search in sources :

Example 41 with Configuration

use of org.hibernate.cfg.Configuration in project querydsl by querydsl.

the class HibernateTestRunner method start.

private void start() throws Exception {
    Configuration cfg = new Configuration();
    for (Class<?> cl : Domain.classes) {
        cfg.addAnnotatedClass(cl);
    }
    String mode = Mode.mode.get() + ".properties";
    isDerby = mode.contains("derby");
    if (isDerby) {
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
    }
    Properties props = new Properties();
    InputStream is = HibernateTestRunner.class.getResourceAsStream(mode);
    if (is == null) {
        throw new IllegalArgumentException("No configuration available at classpath:" + mode);
    }
    props.load(is);
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(props).build();
    cfg.setProperties(props);
    sessionFactory = cfg.buildSessionFactory(serviceRegistry);
    session = sessionFactory.openSession();
    session.beginTransaction();
}
Also used : Configuration(org.hibernate.cfg.Configuration) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) InputStream(java.io.InputStream) ServiceRegistry(org.hibernate.service.ServiceRegistry) Properties(java.util.Properties)

Example 42 with Configuration

use of org.hibernate.cfg.Configuration in project dropwizard by dropwizard.

the class SessionFactoryFactory method buildSessionFactory.

private SessionFactory buildSessionFactory(HibernateBundle<?> bundle, PooledDataSourceFactory dbConfig, ConnectionProvider connectionProvider, Map<String, String> properties, List<Class<?>> entities) {
    final Configuration configuration = new Configuration();
    configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "managed");
    configuration.setProperty(AvailableSettings.USE_SQL_COMMENTS, Boolean.toString(dbConfig.isAutoCommentsEnabled()));
    configuration.setProperty(AvailableSettings.USE_GET_GENERATED_KEYS, "true");
    configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
    configuration.setProperty(AvailableSettings.USE_REFLECTION_OPTIMIZER, "true");
    configuration.setProperty(AvailableSettings.ORDER_UPDATES, "true");
    configuration.setProperty(AvailableSettings.ORDER_INSERTS, "true");
    configuration.setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
    configuration.setProperty("jadira.usertype.autoRegisterUserTypes", "true");
    for (Map.Entry<String, String> property : properties.entrySet()) {
        configuration.setProperty(property.getKey(), property.getValue());
    }
    addAnnotatedClasses(configuration, entities);
    bundle.configure(configuration);
    final ServiceRegistry registry = new StandardServiceRegistryBuilder().addService(ConnectionProvider.class, connectionProvider).applySettings(configuration.getProperties()).build();
    configure(configuration, registry);
    return configuration.buildSessionFactory(registry);
}
Also used : Configuration(org.hibernate.cfg.Configuration) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ServiceRegistry(org.hibernate.service.ServiceRegistry) Map(java.util.Map)

Example 43 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 44 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 45 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)190 Test (org.junit.Test)66 Session (org.hibernate.Session)37 SessionFactory (org.hibernate.SessionFactory)35 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)27 Before (org.junit.Before)20 BeforeClass (org.junit.BeforeClass)20 File (java.io.File)18 ServiceRegistry (org.hibernate.service.ServiceRegistry)14 Properties (java.util.Properties)13 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)9 HibernateException (org.hibernate.HibernateException)8 EncapsulatedCompositeIdResultSetProcessorTest (org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)8 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