Search in sources :

Example 91 with Transaction

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

Example 92 with Transaction

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

Example 93 with Transaction

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

Example 94 with Transaction

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

Example 95 with Transaction

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();
}
Also used : 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