Search in sources :

Example 71 with Transaction

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

Example 72 with Transaction

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

Example 73 with Transaction

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

Example 74 with Transaction

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

Example 75 with Transaction

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

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