use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class BasicHibernateAnnotationsTest method testSerialized.
@Test
@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class)
public void testSerialized() throws Exception {
Forest forest = new Forest();
forest.setName("Shire");
Country country = new Country();
country.setName("Middle Earth");
forest.setCountry(country);
Set<Country> near = new HashSet<Country>();
country = new Country();
country.setName("Mordor");
near.add(country);
country = new Country();
country.setName("Gondor");
near.add(country);
country = new Country();
country.setName("Eriador");
near.add(country);
forest.setNear(near);
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
s.persist(forest);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
forest = (Forest) s.get(Forest.class, forest.getId());
assertNotNull(forest);
country = forest.getCountry();
assertNotNull(country);
assertEquals(country.getName(), forest.getCountry().getName());
near = forest.getNear();
assertTrue("correct number of nearby countries", near.size() == 3);
for (Iterator iter = near.iterator(); iter.hasNext(); ) {
country = (Country) iter.next();
String name = country.getName();
assertTrue("found expected nearby country " + name, (name.equals("Mordor") || name.equals("Gondor") || name.equals("Eriador")));
}
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class BasicHibernateAnnotationsTest method testType.
@Test
@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class)
public void testType() throws Exception {
Forest f = new Forest();
f.setName("Broceliande");
String description = "C'est une enorme foret enchantee ou vivais Merlin et toute la clique";
f.setLongDescription(description);
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
s.persist(f);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
f = (Forest) s.get(Forest.class, f.getId());
assertNotNull(f);
assertEquals(description, f.getLongDescription());
s.delete(f);
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class BasicHibernateAnnotationsTest method testNonLazy.
@Test
public void testNonLazy() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Forest f = new Forest();
Tree t = new Tree();
t.setName("Basic one");
s.persist(f);
s.persist(t);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
f = (Forest) s.load(Forest.class, f.getId());
t = (Tree) s.load(Tree.class, t.getId());
assertFalse("Default should be lazy", Hibernate.isInitialized(f));
assertTrue("Tree is not lazy", Hibernate.isInitialized(t));
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class BasicHibernateAnnotationsTest method testFilter.
@Test
public void testFilter() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
s.createQuery("delete Forest").executeUpdate();
Forest f1 = new Forest();
f1.setLength(2);
s.persist(f1);
Forest f2 = new Forest();
f2.setLength(20);
s.persist(f2);
Forest f3 = new Forest();
f3.setLength(200);
s.persist(f3);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
s.enableFilter("betweenLength").setParameter("minLength", 5).setParameter("maxLength", 50);
long count = ((Long) s.createQuery("select count(*) from Forest").iterate().next()).intValue();
assertEquals(1, count);
s.disableFilter("betweenLength");
s.enableFilter("minLength").setParameter("minLength", 5);
count = ((Long) s.createQuery("select count(*) from Forest").iterate().next()).longValue();
assertEquals(2l, count);
s.disableFilter("minLength");
tx.rollback();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class BasicHibernateAnnotationsTest method testVersioning.
@Test
@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class)
@SkipForDialect(value = TeradataDialect.class, comment = "One transaction hangs the other")
public void testVersioning() throws Exception {
Forest forest = new Forest();
forest.setName("Fontainebleau");
forest.setLength(33);
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
s.persist(forest);
tx.commit();
s.close();
Session parallelSession = openSession();
Transaction parallelTx = parallelSession.beginTransaction();
s = openSession();
tx = s.beginTransaction();
forest = (Forest) parallelSession.get(Forest.class, forest.getId());
Forest reloadedForest = (Forest) s.get(Forest.class, forest.getId());
reloadedForest.setLength(11);
assertNotSame(forest, reloadedForest);
tx.commit();
s.close();
forest.setLength(22);
try {
parallelTx.commit();
fail("All optimistic locking should have make it fail");
} catch (OptimisticLockException e) {
if (parallelTx != null)
parallelTx.rollback();
} finally {
parallelSession.close();
}
s = openSession();
tx = s.beginTransaction();
s.delete(s.get(Forest.class, forest.getId()));
tx.commit();
s.close();
}
Aggregations