use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class ConfigurationTest method testDeclarativeMix.
@Test
public void testDeclarativeMix() throws Exception {
Configuration cfg = new Configuration();
cfg.configure("org/hibernate/test/annotations/hibernate.cfg.xml");
cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
SessionFactory sf = cfg.buildSessionFactory();
assertNotNull(sf);
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Query q = s.createQuery("from Boat");
assertEquals(0, q.list().size());
q = s.createQuery("from Plane");
assertEquals(0, q.list().size());
tx.commit();
s.close();
sf.close();
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class ConfigurationTest method testHbmWithSubclassExtends.
@Test
public void testHbmWithSubclassExtends() throws Exception {
Configuration cfg = new Configuration();
cfg.configure("org/hibernate/test/annotations/hibernate.cfg.xml");
cfg.addClass(Ferry.class);
cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
SessionFactory sf = cfg.buildSessionFactory();
assertNotNull(sf);
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Query q = s.createQuery("from Ferry");
assertEquals(0, q.list().size());
q = s.createQuery("from Plane");
assertEquals(0, q.list().size());
tx.commit();
s.close();
sf.close();
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class SessionFactorySerializationTest method testUnNamedSessionFactorySerialization.
@Test
public void testUnNamedSessionFactorySerialization() throws Exception {
// IMPL NOTE : this test is a control to testNamedSessionFactorySerialization
// here, the test should fail based just on attempted uuid resolution
Configuration cfg = new Configuration().setProperty(AvailableSettings.SESSION_FACTORY_NAME_IS_JNDI, // default is true
"false");
SessionFactory factory = cfg.buildSessionFactory();
// we need to do some tricking here so that Hibernate thinks the deserialization happens in a
// different VM
String uuid = ((SessionFactoryImplementor) factory).getUuid();
// deregister under this uuid...
SessionFactoryRegistry.INSTANCE.removeSessionFactory(uuid, null, false, null);
// and then register under a different uuid...
SessionFactoryRegistry.INSTANCE.addSessionFactory("some-other-uuid", null, false, factory, null);
try {
SerializationHelper.clone(factory);
fail("Expecting an error");
} catch (SerializationException expected) {
}
SessionFactoryRegistry.INSTANCE.removeSessionFactory("some-other-uuid", null, false, null);
factory.close();
assertFalse(SessionFactoryRegistry.INSTANCE.hasRegistrations());
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class SessionFactorySerializationTest method testNamedSessionFactorySerialization.
@Test
public void testNamedSessionFactorySerialization() throws Exception {
Configuration cfg = new Configuration().setProperty(AvailableSettings.SESSION_FACTORY_NAME, NAME).setProperty(AvailableSettings.SESSION_FACTORY_NAME_IS_JNDI, // default is true
"false");
SessionFactory factory = cfg.buildSessionFactory();
// we need to do some tricking here so that Hibernate thinks the deserialization happens in a
// different VM
String uuid = ((SessionFactoryImplementor) factory).getUuid();
// deregister under this uuid...
SessionFactoryRegistry.INSTANCE.removeSessionFactory(uuid, NAME, false, null);
// and then register under a different uuid...
SessionFactoryRegistry.INSTANCE.addSessionFactory("some-other-uuid", NAME, false, factory, null);
SessionFactory factory2 = (SessionFactory) SerializationHelper.clone(factory);
assertSame(factory, factory2);
SessionFactoryRegistry.INSTANCE.removeSessionFactory("some-other-uuid", NAME, false, null);
factory.close();
assertFalse(SessionFactoryRegistry.INSTANCE.hasRegistrations());
}
use of org.hibernate.SessionFactory 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());
}
Aggregations