use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class IdTest method testDefaultSequence.
@Test
public void testDefaultSequence() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Home h = new Home();
s.persist(h);
tx.commit();
s.close();
assertNotNull(h.getId());
s = openSession();
tx = s.beginTransaction();
Home reloadedHome = (Home) s.get(Home.class, h.getId());
assertEquals(h.getId(), reloadedHome.getId());
s.delete(reloadedHome);
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class IdTest method testIdClass.
@Test
public void testIdClass() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Footballer fb = new Footballer("David", "Beckam", "Arsenal");
GoalKeeper keeper = new GoalKeeper("Fabien", "Bartez", "OM");
s.persist(fb);
s.persist(keeper);
tx.commit();
s.clear();
// lookup by id
tx = s.beginTransaction();
FootballerPk fpk = new FootballerPk("David", "Beckam");
fb = (Footballer) s.get(Footballer.class, fpk);
FootballerPk fpk2 = new FootballerPk("Fabien", "Bartez");
keeper = (GoalKeeper) s.get(GoalKeeper.class, fpk2);
assertNotNull(fb);
assertNotNull(keeper);
assertEquals("Beckam", fb.getLastname());
assertEquals("Arsenal", fb.getClub());
assertEquals(1, s.createQuery("from Footballer f where f.firstname = 'David'").list().size());
tx.commit();
// reattach by merge
tx = s.beginTransaction();
fb.setClub("Bimbo FC");
s.merge(fb);
tx.commit();
// reattach by saveOrUpdate
tx = s.beginTransaction();
fb.setClub("Bimbo FC SA");
s.saveOrUpdate(fb);
tx.commit();
// clean up
s.clear();
tx = s.beginTransaction();
fpk = new FootballerPk("David", "Beckam");
fb = (Footballer) s.get(Footballer.class, fpk);
assertEquals("Bimbo FC SA", fb.getClub());
s.delete(fb);
s.delete(keeper);
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class IdTest method testLowAllocationSize.
@Test
public void testLowAllocationSize() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
int size = 4;
BreakDance[] bds = new BreakDance[size];
for (int i = 0; i < size; i++) {
bds[i] = new BreakDance();
s.persist(bds[i]);
}
s.flush();
for (int i = 0; i < size; i++) {
assertEquals(i + 1, bds[i].id.intValue());
}
tx.rollback();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class Java5FeaturesTest method testInterface.
@Test
public void testInterface() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Race r = new Race();
r.setId(new Integer(1));
r.setLength(new Long(3));
s.persist(r);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
r = (Race) s.get(Race.class, r.getId());
assertEquals(new Long(3), r.getLength());
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class Java5FeaturesTest method testAutoboxing.
public void testAutoboxing() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Bid bid = new Bid();
bid.setId(new Integer(2));
bid.setDescription("My best one");
bid.setNote(Starred.OK);
bid.setEditorsNote(Starred.GOOD);
bid.setApproved(null);
s.persist(bid);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
bid = (Bid) s.get(Bid.class, bid.getId());
assertEquals(null, bid.getApproved());
s.delete(bid);
tx.commit();
s.close();
}
Aggregations