use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class BasicHibernateAnnotationsTest method testCache.
@Test
public void testCache() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
ZipCode zc = new ZipCode();
zc.code = "92400";
s.persist(zc);
tx.commit();
s.close();
sessionFactory().getStatistics().clear();
sessionFactory().getStatistics().setStatisticsEnabled(true);
sessionFactory().getCache().evictEntityRegion(ZipCode.class);
s = openSession();
tx = s.beginTransaction();
s.get(ZipCode.class, zc.code);
assertEquals(1, sessionFactory().getStatistics().getSecondLevelCachePutCount());
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
s.get(ZipCode.class, zc.code);
assertEquals(1, sessionFactory().getStatistics().getSecondLevelCacheHitCount());
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class BasicHibernateAnnotationsTest method testCompositeType.
@Test
public void testCompositeType() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Ransom r = new Ransom();
r.setKidnapperName("Se7en");
r.setDate(new Date());
MonetaryAmount amount = new MonetaryAmount(new BigDecimal(100000), Currency.getInstance("EUR"));
r.setAmount(amount);
s.persist(r);
tx.commit();
s.clear();
tx = s.beginTransaction();
r = (Ransom) s.get(Ransom.class, r.getId());
assertNotNull(r);
assertNotNull(r.getAmount());
assertTrue(0 == new BigDecimal(100000).compareTo(r.getAmount().getAmount()));
assertEquals(Currency.getInstance("EUR"), r.getAmount().getCurrency());
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class BasicHibernateAnnotationsTest method testCascadedDeleteOfChildEntitiesBug2.
@Test
public void testCascadedDeleteOfChildEntitiesBug2() {
// Relationship is one SoccerTeam to many Players.
// Create a SoccerTeam (parent) and three Players (child).
// Verify that the count of Players is correct.
// Clear the SoccerTeam reference Players.
// The orphanRemoval should remove the Players automatically.
// @OneToMany(mappedBy="name", orphanRemoval=true)
Session s = openSession();
Transaction tx = s.beginTransaction();
SoccerTeam team = new SoccerTeam();
int teamid = team.getId();
Player player1 = new Player();
player1.setName("Shalrie Joseph");
team.addPlayer(player1);
Player player2 = new Player();
player2.setName("Taylor Twellman");
team.addPlayer(player2);
Player player3 = new Player();
player3.setName("Steve Ralston");
team.addPlayer(player3);
s.persist(team);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
team = (SoccerTeam) s.merge(team);
int count = ((Long) s.createQuery("select count(*) from Player").iterate().next()).intValue();
assertEquals("expected count of 3 but got = " + count, count, 3);
// clear references to players, this should orphan the players which should
// in turn trigger orphanRemoval logic.
team.getPlayers().clear();
// count = ( (Long) s.createQuery( "select count(*) from Player" ).iterate().next() ).intValue();
// assertEquals("expected count of 0 but got = " + count, count, 0);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
count = ((Long) s.createQuery("select count(*) from Player").iterate().next()).intValue();
assertEquals("expected count of 0 but got = " + count, count, 0);
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class IdClassTest method testIdClassInSuperclass.
@Test
public void testIdClassInSuperclass() throws Exception {
Tower tower = new Tower();
tower.latitude = 10.3;
tower.longitude = 45.4;
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist(tower);
s.flush();
s.clear();
Location loc = new Location();
loc.latitude = tower.latitude;
loc.longitude = tower.longitude;
assertNotNull(s.get(Tower.class, loc));
tx.rollback();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class IdTest method testMethodLevelGenerator.
@Test
public void testMethodLevelGenerator() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Department b = new Department();
s.persist(b);
tx.commit();
s.close();
assertNotNull(b.getId());
s = openSession();
tx = s.beginTransaction();
s.delete(s.get(Department.class, b.getId()));
tx.commit();
s.close();
}
Aggregations