Search in sources :

Example 91 with MetadataSources

use of org.hibernate.boot.MetadataSources in project jbosstools-hibernate by jbosstools.

the class ConfigurationFacadeTest method testAddFile.

@Test
public void testAddFile() throws Exception {
    File testFile = File.createTempFile("test", "hbm.xml");
    PrintWriter printWriter = new PrintWriter(testFile);
    printWriter.write(TEST_HBM_XML_STRING);
    printWriter.close();
    MetadataSources metadataSources = MetadataHelper.getMetadataSources(configuration);
    Assert.assertTrue(metadataSources.getXmlBindings().isEmpty());
    Assert.assertSame(configurationFacade, configurationFacade.addFile(testFile));
    Assert.assertFalse(metadataSources.getXmlBindings().isEmpty());
    Binding<?> binding = metadataSources.getXmlBindings().iterator().next();
    Assert.assertEquals(testFile.getAbsolutePath(), binding.getOrigin().getName());
    Assert.assertTrue(testFile.delete());
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 92 with MetadataSources

use of org.hibernate.boot.MetadataSources in project hibernate-orm by hibernate.

the class InterceptorTest method testConfiguredSessionInterceptorWithSessionFactory.

@Test
public void testConfiguredSessionInterceptorWithSessionFactory() {
    StandardServiceRegistryImpl standardRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().build();
    SessionFactory sessionFactory = null;
    try {
        MetadataSources metadataSources = new MetadataSources(standardRegistry);
        for (Class annotatedClass : getAnnotatedClasses()) {
            metadataSources.addAnnotatedClass(annotatedClass);
        }
        Metadata metadata = metadataSources.getMetadataBuilder().build();
        SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
        sessionFactoryBuilder.applyStatelessInterceptor(LocalExceptionInterceptor.class);
        sessionFactory = sessionFactoryBuilder.build();
        final SessionFactory sessionFactoryInstance = sessionFactory;
        Supplier<SessionFactory> sessionFactorySupplier = () -> sessionFactoryInstance;
        Item i = new Item();
        i.setName("Laptop");
        try {
            doInHibernate(sessionFactorySupplier, session -> {
                session.persist(i);
                fail("No interceptor");
                return null;
            });
        } catch (IllegalStateException e) {
            assertEquals(LocalExceptionInterceptor.LOCAL_EXCEPTION_MESSAGE, e.getMessage());
        }
    } finally {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
        standardRegistry.destroy();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Item(org.hibernate.jpa.test.Item) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) Test(org.junit.Test)

Example 93 with MetadataSources

use of org.hibernate.boot.MetadataSources 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 94 with MetadataSources

use of org.hibernate.boot.MetadataSources 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 95 with MetadataSources

use of org.hibernate.boot.MetadataSources 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)

Aggregations

MetadataSources (org.hibernate.boot.MetadataSources)293 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)193 Test (org.junit.Test)192 Metadata (org.hibernate.boot.Metadata)123 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)120 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)76 TestForIssue (org.hibernate.testing.TestForIssue)57 PersistentClass (org.hibernate.mapping.PersistentClass)53 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)41 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)37 Before (org.junit.Before)33 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)30 ServiceRegistry (org.hibernate.service.ServiceRegistry)26 RootClass (org.hibernate.mapping.RootClass)25 HashMap (java.util.HashMap)20 SimpleValue (org.hibernate.mapping.SimpleValue)20 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)20 File (java.io.File)19 Property (org.hibernate.mapping.Property)18 Map (java.util.Map)17