Search in sources :

Example 66 with Transaction

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();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 67 with Transaction

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();
}
Also used : Transaction(org.hibernate.Transaction) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 68 with Transaction

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();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 69 with Transaction

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();
}
Also used : Transaction(org.hibernate.Transaction) Tower(org.hibernate.test.annotations.id.entities.Tower) Session(org.hibernate.Session) Location(org.hibernate.test.annotations.id.entities.Location) Test(org.junit.Test)

Example 70 with Transaction

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();
}
Also used : Department(org.hibernate.test.annotations.id.entities.Department) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Transaction (org.hibernate.Transaction)1273 Session (org.hibernate.Session)1251 Test (org.junit.Test)1124 List (java.util.List)239 ArrayList (java.util.ArrayList)143 Iterator (java.util.Iterator)87 TestForIssue (org.hibernate.testing.TestForIssue)87 Query (org.hibernate.Query)80 BigDecimal (java.math.BigDecimal)73 Date (java.util.Date)52 HashSet (java.util.HashSet)44 Criteria (org.hibernate.Criteria)39 SkipForDialect (org.hibernate.testing.SkipForDialect)36 ScrollableResults (org.hibernate.ScrollableResults)35 Set (java.util.Set)30 PersistenceException (javax.persistence.PersistenceException)30 HashMap (java.util.HashMap)29 Map (java.util.Map)25 FailureExpected (org.hibernate.testing.FailureExpected)23 Serializable (java.io.Serializable)22