use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class EmbeddedTest method testEmbeddedInSecondaryTable.
@Test
public void testEmbeddedInSecondaryTable() throws Exception {
Session s;
s = openSession();
s.getTransaction().begin();
Book book = new Book();
book.setIsbn("1234");
book.setName("HiA Second Edition");
Summary summary = new Summary();
summary.setText("This is a HiA SE summary");
summary.setSize(summary.getText().length());
book.setSummary(summary);
s.persist(book);
s.getTransaction().commit();
s.clear();
Transaction tx = s.beginTransaction();
Book loadedBook = (Book) s.get(Book.class, book.getIsbn());
assertNotNull(loadedBook.getSummary());
assertEquals(book.getSummary().getText(), loadedBook.getSummary().getText());
s.delete(loadedBook);
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class FetchingTest method testHibernateFetchingLazy.
@Test
public void testHibernateFetchingLazy() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Person p = new Person("Gavin", "King", "JBoss Inc");
Stay stay = new Stay(null, new Date(), new Date(), "A380", "Blah", "Blah");
Stay stay2 = new Stay(null, new Date(), new Date(), "A320", "Blah", "Blah");
Stay stay3 = new Stay(null, new Date(), new Date(), "A340", "Blah", "Blah");
stay.setOldPerson(p);
stay2.setVeryOldPerson(p);
stay3.setVeryOldPerson(p);
p.addOldStay(stay);
p.addVeryOldStay(stay2);
p.addVeryOldStay(stay3);
s.persist(p);
tx.commit();
s.clear();
tx = s.beginTransaction();
p = (Person) s.createQuery("from Person p where p.firstName = :name").setParameter("name", "Gavin").uniqueResult();
assertFalse(Hibernate.isInitialized(p.getOldStays()));
assertEquals(1, p.getOldStays().size());
assertFalse("lazy extra is failing", Hibernate.isInitialized(p.getOldStays()));
s.clear();
stay = (Stay) s.get(Stay.class, stay.getId());
assertTrue(!Hibernate.isInitialized(stay.getOldPerson()));
s.clear();
stay3 = (Stay) s.get(Stay.class, stay3.getId());
assertTrue("FetchMode.JOIN should overrides lazy options", Hibernate.isInitialized(stay3.getVeryOldPerson()));
s.delete(stay3.getVeryOldPerson());
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class FetchingTest method testExtraLazy.
@Test
public void testExtraLazy() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Person p = new Person("Gavin", "King", "JBoss Inc");
Stay stay = new Stay(p, new Date(), new Date(), "A380", "Blah", "Blah");
p.getOrderedStay().add(stay);
s.persist(p);
tx.commit();
s.clear();
tx = s.beginTransaction();
p = (Person) s.createQuery("from Person p where p.firstName = :name").setParameter("name", "Gavin").uniqueResult();
assertFalse(Hibernate.isInitialized(p.getOrderedStay()));
assertEquals(1, p.getOrderedStay().size());
assertFalse(Hibernate.isInitialized(p.getOrderedStay()));
assertEquals("A380", p.getOrderedStay().get(0).getVessel());
assertFalse(Hibernate.isInitialized(p.getOrderedStay()));
s.delete(p);
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class EnumIdTest method testEnumAsId.
@Test
public void testEnumAsId() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
PlanetCheatSheet mercury = new PlanetCheatSheet();
mercury.setPlanet(Planet.MERCURY);
mercury.setMass(3.303e+23);
mercury.setRadius(2.4397e6);
mercury.setNumberOfInhabitants(0);
s.persist(mercury);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
PlanetCheatSheet mercuryFromDb = (PlanetCheatSheet) s.get(PlanetCheatSheet.class, mercury.getPlanet());
assertNotNull(mercuryFromDb);
log.debug(mercuryFromDb.toString());
s.delete(mercuryFromDb);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
mercury = (PlanetCheatSheet) s.get(PlanetCheatSheet.class, Planet.MERCURY);
assertNull(mercury);
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class HibernateSequenceTest method testHibernateSequenceNextVal.
@Test
public void testHibernateSequenceNextVal() {
Session session = openSession();
Transaction txn = session.beginTransaction();
HibernateSequenceEntity entity = new HibernateSequenceEntity();
entity.setText("sample text");
session.save(entity);
txn.commit();
session.close();
Assert.assertNotNull(entity.getId());
}
Aggregations