use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class NamedQueryTest method testQuery.
@Test
public void testQuery() {
Configuration cfg = new Configuration();
cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
cfg.addInputStream(new ReaderInputStream(new StringReader(NAMED_QUERY_HBM_XML)));
SessionFactory sessionFactory = cfg.buildSessionFactory();
sessionFactory.close();
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class GeneratedValueTest method testGeneratedUuidId.
@Test
public void testGeneratedUuidId() throws Exception {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop").build();
try {
Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata();
((MetadataImpl) metadata).validate();
PersistentClass entityBinding = metadata.getEntityBinding(TheEntity.class.getName());
assertEquals(UUID.class, entityBinding.getIdentifier().getType().getReturnedClass());
IdentifierGenerator generator = entityBinding.getIdentifier().createIdentifierGenerator(metadata.getIdentifierGeneratorFactory(), metadata.getDatabase().getDialect(), null, null, (RootClass) entityBinding);
assertTyping(UUIDGenerator.class, generator);
// now a functional test
SessionFactory sf = metadata.buildSessionFactory();
try {
TheEntity theEntity = new TheEntity();
Session s = sf.openSession();
s.beginTransaction();
s.save(theEntity);
s.getTransaction().commit();
s.close();
assertNotNull(theEntity.id);
s = sf.openSession();
s.beginTransaction();
try {
s.delete(theEntity);
s.getTransaction().commit();
} catch (Exception e) {
s.getTransaction().rollback();
throw e;
} finally {
s.close();
}
} finally {
try {
sf.close();
} catch (Exception ignore) {
}
}
} finally {
StandardServiceRegistryBuilder.destroy(ssr);
}
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class DeepCollectionElementTest method testInitialization.
@Test
public void testInitialization() throws Exception {
Configuration configuration = new Configuration();
configuration.addAnnotatedClass(A.class);
configuration.addAnnotatedClass(B.class);
configuration.addAnnotatedClass(C.class);
StandardServiceRegistryImpl serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(configuration.getProperties());
try {
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
sessionFactory.close();
} finally {
serviceRegistry.destroy();
}
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class OneToOneErrorTest method testWrongOneToOne.
@Test
public void testWrongOneToOne() throws Exception {
Configuration cfg = new Configuration();
cfg.addAnnotatedClass(Show.class).addAnnotatedClass(ShowDescription.class);
cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
ServiceRegistry serviceRegistry = null;
SessionFactory sessionFactory = null;
try {
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(Environment.getProperties());
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
Assert.fail("Wrong mappedBy does not fail property");
} catch (AnnotationException e) {
//success
} finally {
if (sessionFactory != null) {
sessionFactory.close();
}
if (serviceRegistry != null) {
ServiceRegistryBuilder.destroy(serviceRegistry);
}
}
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class AttributeConverterTest method testBasicTimestampUsage.
@Test
public void testBasicTimestampUsage() {
Configuration cfg = new Configuration();
cfg.addAttributeConverter(InstantConverter.class, false);
cfg.addAnnotatedClass(IrrelevantInstantEntity.class);
cfg.setProperty(AvailableSettings.HBM2DDL_AUTO, "create-drop");
cfg.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
SessionFactory sf = cfg.buildSessionFactory();
try {
Session session = sf.openSession();
session.beginTransaction();
session.save(new IrrelevantInstantEntity(1L));
session.getTransaction().commit();
session.close();
sf.getStatistics().clear();
session = sf.openSession();
session.beginTransaction();
IrrelevantInstantEntity e = (IrrelevantInstantEntity) session.get(IrrelevantInstantEntity.class, 1L);
session.getTransaction().commit();
session.close();
assertEquals(0, sf.getStatistics().getEntityUpdateCount());
session = sf.openSession();
session.beginTransaction();
session.delete(e);
session.getTransaction().commit();
session.close();
} finally {
sf.close();
}
}
Aggregations