use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class ManyToManyTest method testDefaultCompositePk.
@Test
public void testDefaultCompositePk() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
CatPk catPk = new CatPk();
catPk.setName("Minou");
catPk.setThoroughbred("Persan");
Cat cat = new Cat();
cat.setId(catPk);
cat.setAge(32);
Woman woman = new Woman();
WomanPk womanPk = new WomanPk();
womanPk.setFirstName("Emma");
womanPk.setLastName("Peel");
woman.setId(womanPk);
woman.setCats(new HashSet<Cat>());
woman.getCats().add(cat);
cat.setHumanContacts(new HashSet<Woman>());
cat.getHumanContacts().add(woman);
s.persist(woman);
s.persist(cat);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
Cat sameCat = (Cat) s.get(Cat.class, cat.getId());
assertNotNull(sameCat);
assertNotNull(sameCat.getHumanContacts());
assertEquals(1, sameCat.getHumanContacts().size());
Woman sameWoman = sameCat.getHumanContacts().iterator().next();
assertEquals(sameWoman.getId().getLastName(), woman.getId().getLastName());
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
sameWoman = (Woman) s.get(Woman.class, woman.getId());
assertNotNull(sameWoman);
assertNotNull(sameWoman.getCats());
assertEquals(1, sameWoman.getCats().size());
sameCat = sameWoman.getCats().iterator().next();
assertEquals(cat.getAge(), sameCat.getAge());
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class ManyToOneJoinTest method testOneToOneJoinTable.
@Test
public void testOneToOneJoinTable() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
ForestType forest = new ForestType();
forest.setName("Original forest");
s.persist(forest);
BiggestForest forestRepr = new BiggestForest();
forestRepr.setType(forest);
forest.setBiggestRepresentative(forestRepr);
s.persist(forestRepr);
s.flush();
s.clear();
forest = (ForestType) s.get(ForestType.class, forest.getId());
assertNotNull(forest.getBiggestRepresentative());
assertEquals(forest, forest.getBiggestRepresentative().getType());
tx.rollback();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class NamingStrategyJoinTest method testJoinToSecondaryTable.
@Test
public void testJoinToSecondaryTable() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Life life = new Life();
life.duration = 15;
life.fullDescription = "Long long description";
s.persist(life);
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class LoaderTest method testBasic.
@Test
public void testBasic() throws Exception {
// set up data...
Session s = openSession();
Transaction tx = s.beginTransaction();
Team t = new Team();
Player p = new Player();
p.setName("me");
t.getPlayers().add(p);
p.setTeam(t);
s.persist(p);
s.persist(t);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
Team t2 = s.load(Team.class, t.getId());
Set<Player> players = t2.getPlayers();
Iterator<Player> iterator = players.iterator();
assertEquals("me", iterator.next().getName());
tx.commit();
s.close();
// clean up data
s = openSession();
tx = s.beginTransaction();
t = s.get(Team.class, t2.getId());
p = s.get(Player.class, p.getId());
s.delete(p);
s.delete(t);
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class AbstractLobTest method testBlob.
@Test
public void testBlob() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
C cc = createCompiledCode();
Byte[] header = new Byte[2];
header[0] = new Byte((byte) 3);
header[1] = new Byte((byte) 0);
cc.setHeader(header);
int codeSize = 5;
byte[] full = new byte[codeSize];
for (int i = 0; i < codeSize; i++) {
full[i] = (byte) (1 + i);
}
cc.setFullCode(full);
s.persist(cc);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
C recompiled = getCompiledCodeClass().cast(s.get(getCompiledCodeClass(), getId(cc)));
assertEquals(recompiled.getHeader()[1], cc.getHeader()[1]);
assertEquals(recompiled.getFullCode()[codeSize - 1], cc.getFullCode()[codeSize - 1]);
tx.commit();
s.close();
}
Aggregations