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