Search in sources :

Example 1 with TheEntity

use of org.hibernate.test.cdi.converters.TheEntity in project hibernate-orm by hibernate.

the class DelayedCdiHostedConverterTest method testIt.

@Test
public void testIt() {
    MonitorBean.reset();
    final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(MonitorBean.class, ConverterBean.class);
    try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
        BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
        final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager()).applySetting(AvailableSettings.DELAY_CDI_ACCESS, "true").build();
        final SessionFactoryImplementor sessionFactory;
        try {
            sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata().getSessionFactoryBuilder().build();
        } catch (Exception e) {
            StandardServiceRegistryBuilder.destroy(ssr);
            throw e;
        }
        // The CDI bean should not have been built immediately...
        assertFalse(MonitorBean.wasInstantiated());
        assertEquals(0, MonitorBean.currentFromDbCount());
        assertEquals(0, MonitorBean.currentToDbCount());
        try {
            inTransaction(sessionFactory, session -> session.persist(new TheEntity(1, "me", 5)));
            // The CDI bean should have been built on first use
            assertTrue(MonitorBean.wasInstantiated());
            assertEquals(0, MonitorBean.currentFromDbCount());
            assertEquals(1, MonitorBean.currentToDbCount());
            inTransaction(sessionFactory, session -> {
                TheEntity it = session.find(TheEntity.class, 1);
                assertNotNull(it);
            });
            assertEquals(1, MonitorBean.currentFromDbCount());
            assertEquals(1, MonitorBean.currentToDbCount());
        } finally {
            inTransaction(sessionFactory, session -> {
                session.createQuery("delete TheEntity").executeUpdate();
            });
            sessionFactory.close();
        }
    }
}
Also used : TheEntity(org.hibernate.test.cdi.converters.TheEntity) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) SeContainer(javax.enterprise.inject.se.SeContainer) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) SeContainerInitializer(javax.enterprise.inject.se.SeContainerInitializer) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 2 with TheEntity

use of org.hibernate.test.cdi.converters.TheEntity in project hibernate-orm by hibernate.

the class CdiHostedConverterTest method testIt.

@Test
public void testIt() {
    MonitorBean.reset();
    final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(MonitorBean.class, ConverterBean.class);
    try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
        BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
        final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager()).build();
        final SessionFactoryImplementor sessionFactory;
        try {
            sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata().getSessionFactoryBuilder().build();
        } catch (Exception e) {
            StandardServiceRegistryBuilder.destroy(ssr);
            throw e;
        }
        // The CDI bean should have been built immediately...
        assertTrue(MonitorBean.wasInstantiated());
        assertEquals(0, MonitorBean.currentFromDbCount());
        assertEquals(0, MonitorBean.currentToDbCount());
        try {
            inTransaction(sessionFactory, session -> session.persist(new TheEntity(1, "me", 5)));
            assertEquals(0, MonitorBean.currentFromDbCount());
            assertEquals(1, MonitorBean.currentToDbCount());
            inTransaction(sessionFactory, session -> {
                TheEntity it = session.find(TheEntity.class, 1);
                assertNotNull(it);
            });
            assertEquals(1, MonitorBean.currentFromDbCount());
            assertEquals(1, MonitorBean.currentToDbCount());
        } finally {
            inTransaction(sessionFactory, session -> {
                session.createQuery("delete TheEntity").executeUpdate();
            });
            sessionFactory.close();
        }
    }
}
Also used : TheEntity(org.hibernate.test.cdi.converters.TheEntity) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) SeContainer(javax.enterprise.inject.se.SeContainer) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) SeContainerInitializer(javax.enterprise.inject.se.SeContainerInitializer) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Aggregations

SeContainer (javax.enterprise.inject.se.SeContainer)2 SeContainerInitializer (javax.enterprise.inject.se.SeContainerInitializer)2 MetadataSources (org.hibernate.boot.MetadataSources)2 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)2 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)2 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)2 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 TheEntity (org.hibernate.test.cdi.converters.TheEntity)2 Test (org.junit.Test)2