use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.
the class TemplateTest method buildSessionFactory.
@BeforeClass
public static void buildSessionFactory() {
Configuration cfg = new Configuration();
cfg.setProperty(AvailableSettings.DIALECT, DIALECT.getClass().getName());
ServiceRegistry serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(cfg.getProperties());
SESSION_FACTORY = (SessionFactoryImplementor) cfg.buildSessionFactory(serviceRegistry);
}
use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.
the class PersisterClassProviderTest method testPersisterClassProvider.
@Test
public void testPersisterClassProvider() throws Exception {
Configuration cfg = new Configuration();
cfg.addAnnotatedClass(Gate.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
//no exception as the GoofyPersisterClassProvider is not set
SessionFactory sessionFactory;
try {
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
sessionFactory.close();
} finally {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).addService(PersisterClassResolver.class, new GoofyPersisterClassProvider()).build();
cfg = new Configuration();
cfg.addAnnotatedClass(Gate.class);
try {
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
sessionFactory.close();
fail("The entity persister should be overridden");
} catch (MappingException e) {
assertEquals("The entity persister should be overridden", GoofyPersisterClassProvider.NoopEntityPersister.class, ((GoofyException) e.getCause()).getValue());
} finally {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
assertFalse(SessionFactoryRegistry.INSTANCE.hasRegistrations());
cfg = new Configuration();
cfg.addAnnotatedClass(Portal.class);
cfg.addAnnotatedClass(Window.class);
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).addService(PersisterClassResolver.class, new GoofyPersisterClassProvider()).build();
try {
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
sessionFactory.close();
fail("The collection persister should be overridden but not the entity persister");
} catch (MappingException e) {
assertEquals("The collection persister should be overridden but not the entity persister", GoofyPersisterClassProvider.NoopCollectionPersister.class, ((GoofyException) e.getCause()).getValue());
} finally {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
cfg = new Configuration();
cfg.addAnnotatedClass(Tree.class);
cfg.addAnnotatedClass(Palmtree.class);
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).addService(PersisterClassResolver.class, new GoofyPersisterClassProvider()).build();
try {
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
sessionFactory.close();
fail("The entity persisters should be overridden in a class hierarchy");
} catch (MappingException e) {
assertEquals("The entity persisters should be overridden in a class hierarchy", GoofyPersisterClassProvider.NoopEntityPersister.class, ((GoofyException) e.getCause()).getValue());
} finally {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
assertFalse(SessionFactoryRegistry.INSTANCE.hasRegistrations());
}
use of org.hibernate.service.ServiceRegistry in project wildfly by wildfly.
the class SFSBHibernateSFNaturalId method setupConfig.
public void setupConfig() {
// static {
try {
// prepare the configuration
Configuration configuration = new Configuration().setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
configuration.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
configuration.setProperty(Environment.DATASOURCE, "java:jboss/datasources/ExampleDS");
// configuration.configure("hibernate.cfg.xml");
// fetch the properties
Properties properties = new Properties();
properties.putAll(configuration.getProperties());
Environment.verifyProperties(properties);
ConfigurationHelper.resolvePlaceHolders(properties);
// build the serviceregistry
final BootstrapServiceRegistryBuilder bootstrapbuilder = new BootstrapServiceRegistryBuilder();
ServiceRegistry serviceRegistry = builder.build();
// Create the SessionFactory from Configuration
sessionFactory = configuration.configure("hibernate.cfg.xml").buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
throw new RuntimeException("Could not setup config", ex);
}
}
Aggregations