Search in sources :

Example 31 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class CascadeTest method testDetach.

@Test
public void testDetach() {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    Tooth tooth = new Tooth();
    Mouth mouth = new Mouth();
    s.persist(mouth);
    s.persist(tooth);
    tooth.mouth = mouth;
    mouth.teeth = new ArrayList<Tooth>();
    mouth.teeth.add(tooth);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    mouth = (Mouth) s.get(Mouth.class, mouth.id);
    assertNotNull(mouth);
    assertEquals(1, mouth.teeth.size());
    tooth = mouth.teeth.iterator().next();
    s.evict(mouth);
    assertFalse(s.contains(tooth));
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    s.delete(s.get(Mouth.class, mouth.id));
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 32 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class CascadeTest method testPersist.

@Test
public void testPersist() {
    Session s;
    Transaction tx;
    s = openSession();
    Tooth tooth = new Tooth();
    Tooth leftTooth = new Tooth();
    tooth.leftNeighbour = leftTooth;
    s.persist(tooth);
    tx = s.beginTransaction();
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    leftTooth = (Tooth) s.get(Tooth.class, leftTooth.id);
    assertNotNull(leftTooth);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 33 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class CascadeToEmbeddedManyToOneTest method testPersistCascadeToEmbedded.

@Test
public void testPersistCascadeToEmbedded() {
    Session sess = openSession();
    try {
        final Transaction trx = sess.beginTransaction();
        try {
            PersonPair personPair = new PersonPair(new Person("PERSON NAME 1"), new Person("PERSON NAME 2"));
            sess.persist(new CodedPairHolder("CODE", personPair));
            sess.flush();
        } finally {
            trx.rollback();
        }
    } finally {
        sess.close();
    }
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 34 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class CascadeToEmbeddedManyToOneTest method testPersistCascadeToSetOfEmbedded.

@Test
public void testPersistCascadeToSetOfEmbedded() {
    Session sess = openSession();
    try {
        final Transaction trx = sess.beginTransaction();
        try {
            final Set<PersonPair> setOfPairs = new HashSet<PersonPair>();
            setOfPairs.add(new PersonPair(new Person("PERSON NAME 1"), new Person("PERSON NAME 2")));
            sess.persist(new CodedPairSetHolder("CODE", setOfPairs));
            sess.flush();
        } finally {
            trx.rollback();
        }
    } finally {
        sess.close();
    }
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 35 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class AccessTest method testEmbeddableExplicitAccessStrategy.

@Test
public void testEmbeddableExplicitAccessStrategy() throws Exception {
    Square square = new Square();
    Position pos = new Position(10, 15);
    square.setPosition(pos);
    Session s = openSession();
    s.persist(square);
    Transaction tx = s.beginTransaction();
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    square = (Square) s.get(Square.class, square.getId());
    assertEquals(10, square.getPosition().x);
    try {
        square.getPosition().getX();
        fail();
    } catch (RuntimeException e) {
    // success
    }
    s.delete(square);
    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