Search in sources :

Example 56 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.

the class DefaultCacheConcurrencyPropertyTest method testExplicitDefault.

@Test
@TestForIssue(jiraKey = "HHH-9763")
@FailureExpected(jiraKey = "HHH-9763")
public void testExplicitDefault() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY, "read-only").build();
    try {
        assertEquals("read-only", ssr.getService(ConfigurationService.class).getSettings().get(AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY));
        final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata();
        assertEquals(AccessType.READ_ONLY, metadata.getMetadataBuildingOptions().getMappingDefaults().getImplicitCacheAccessType());
        final SessionFactoryImplementor sf = (SessionFactoryImplementor) metadata.buildSessionFactory();
        try {
            final EntityPersister persister = sf.getMetamodel().entityPersister(TheEntity.class.getName());
            assertTrue(persister.canReadFromCache());
            assertTrue(persister.canWriteToCache());
            assertNotNull(persister.getCacheAccessStrategy());
        } finally {
            sf.close();
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected) TestForIssue(org.hibernate.testing.TestForIssue)

Example 57 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.

the class ValidExtendedCdiSupportTest method testIt.

@Test
public void testIt() {
    Monitor.reset();
    final ExtendedBeanManagerImpl standIn = new ExtendedBeanManagerImpl();
    BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, standIn).build();
    final SessionFactoryImplementor sessionFactory;
    try {
        sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata().getSessionFactoryBuilder().build();
    } catch (Exception e) {
        StandardServiceRegistryBuilder.destroy(ssr);
        throw e;
    }
    try {
        // The CDI bean should not be built immediately...
        assertFalse(Monitor.wasInstantiated());
        assertEquals(0, Monitor.currentCount());
        // But now lets initialize CDI and do the callback
        final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(Monitor.class, TheListener.class);
        try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
            standIn.beanManagerReady(cdiContainer.getBeanManager());
            // at this point the bean should have been accessed
            assertTrue(Monitor.wasInstantiated());
            assertEquals(0, Monitor.currentCount());
            try {
                inTransaction(sessionFactory, session -> session.persist(new TheEntity(1)));
                inTransaction(sessionFactory, session -> {
                    TheEntity it = session.find(TheEntity.class, 1);
                    assertNotNull(it);
                });
            } finally {
                inTransaction(sessionFactory, session -> {
                    session.createQuery("delete TheEntity").executeUpdate();
                });
            }
        }
    } finally {
        sessionFactory.close();
    }
}
Also used : TheEntity(org.hibernate.test.cdi.events.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) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SeContainerInitializer(javax.enterprise.inject.se.SeContainerInitializer) Test(org.junit.Test)

Example 58 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.

the class ExtendedMixedAccessTest method testExtendedMixedAccess.

@Test
public void testExtendedMixedAccess() {
    final Helper.TestingExtendedBeanManager extendedBeanManager = Helper.createExtendedBeanManager();
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, extendedBeanManager).build();
    final BeanContainer beanContainer = ssr.getService(ManagedBeanRegistry.class).getBeanContainer();
    assertThat(beanContainer, instanceOf(CdiBeanContainerExtendedAccessImpl.class));
    try (final SeContainer cdiContainer = Helper.createSeContainer()) {
        final BeanManager beanManager = cdiContainer.getBeanManager();
        extendedBeanManager.notifyListenerReady(beanManager);
        assertThat(beanManager, sameInstance(((CdiBeanContainerExtendedAccessImpl) beanContainer).getUsableBeanManager()));
        final ContainedBean<HostedBean> hostedBean = beanContainer.getBean(HostedBean.class, this, FallbackBeanInstanceProducer.INSTANCE);
        assertThat(hostedBean, notNullValue());
        assertThat(hostedBean.getBeanInstance(), notNullValue());
        assertThat(hostedBean.getBeanInstance().getInjectedHostedBean(), notNullValue());
        final ContainedBean<NonHostedBean> nonHostedBean = beanContainer.getBean(NonHostedBean.class, this, FallbackBeanInstanceProducer.INSTANCE);
        assertThat(nonHostedBean, notNullValue());
        assertThat(nonHostedBean.getBeanInstance(), notNullValue());
        extendedBeanManager.notifyListenerShuttingDown(beanManager);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) CdiBeanContainerExtendedAccessImpl(org.hibernate.resource.beans.container.internal.CdiBeanContainerExtendedAccessImpl) ManagedBeanRegistry(org.hibernate.resource.beans.spi.ManagedBeanRegistry) BeanContainer(org.hibernate.resource.beans.container.spi.BeanContainer) SeContainer(javax.enterprise.inject.se.SeContainer) BeanManager(javax.enterprise.inject.spi.BeanManager) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 59 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.

the class InvalidExtendedCdiSupportTest method testIt.

@Test
public void testIt() {
    Monitor.reset();
    final ExtendedBeanManagerImpl standIn = new ExtendedBeanManagerImpl();
    BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, standIn).build();
    final SessionFactoryImplementor sessionFactory;
    try {
        sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata().getSessionFactoryBuilder().build();
    } catch (Exception e) {
        StandardServiceRegistryBuilder.destroy(ssr);
        throw e;
    }
    try {
        // The CDI bean should not be built immediately...
        assertFalse(Monitor.wasInstantiated());
        assertEquals(0, Monitor.currentCount());
        try {
            inTransaction(sessionFactory, session -> session.persist(new TheEntity(1)));
            inTransaction(sessionFactory, session -> {
                session.createQuery("delete TheEntity").executeUpdate();
            });
            fail("Expecting failure");
        } catch (IllegalStateException expected) {
        }
    } finally {
        sessionFactory.close();
    }
}
Also used : TheEntity(org.hibernate.test.cdi.events.TheEntity) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 60 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.

the class InvalidNoCdiSupportTest method testIt.

@Test
public void testIt() {
    Monitor.reset();
    BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).build();
    final SessionFactoryImplementor sessionFactory;
    try {
        // because there is no CDI available, building the SF should immediately
        // try to build the ManagedBeans which should fail here
        sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata().getSessionFactoryBuilder().build();
        sessionFactory.close();
        fail("Expecting failure");
    } catch (Exception e) {
        StandardServiceRegistryBuilder.destroy(ssr);
        assertThat(e, instanceOf(InstantiationException.class));
    }
}
Also used : TheEntity(org.hibernate.test.cdi.events.TheEntity) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) InstantiationException(org.hibernate.InstantiationException) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Aggregations

StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)169 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)152 Test (org.junit.Test)121 MetadataSources (org.hibernate.boot.MetadataSources)118 Metadata (org.hibernate.boot.Metadata)56 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)39 PersistentClass (org.hibernate.mapping.PersistentClass)39 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)25 TestForIssue (org.hibernate.testing.TestForIssue)24 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)23 Properties (java.util.Properties)20 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)20 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)15 SessionFactory (org.hibernate.SessionFactory)14 Column (org.hibernate.mapping.Column)14 Property (org.hibernate.mapping.Property)14 Session (org.hibernate.Session)13 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)12 IdentifierGenerator (org.hibernate.id.IdentifierGenerator)11 RootClass (org.hibernate.mapping.RootClass)11