Search in sources :

Example 6 with Transaction

use of org.hibernate.Transaction in project druid by alibaba.

the class HibernateCRUDTest method test_transactional_delete.

public void test_transactional_delete() {
    Session session = null;
    Transaction tran = null;
    try {
        session = sessionFactory.openSession();
        tran = session.beginTransaction();
        doCreate(session);
        doDelete(session);
    } finally {
        if (tran != null) {
            tran.commit();
        }
        if (session != null) {
            session.flush();
            session.close();
        }
    }
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session)

Example 7 with Transaction

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

the class SpatialFunctionalTestCase method cleanUpTest.

private void cleanUpTest(String pckg) {
    Session session = null;
    Transaction tx = null;
    try {
        session = openSession();
        tx = session.beginTransaction();
        String hql = String.format("delete from org.hibernate.spatial.integration.%s.GeomEntity", pckg);
        Query q = session.createQuery(hql);
        q.executeUpdate();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) BatchUpdateException(java.sql.BatchUpdateException) SQLException(java.sql.SQLException) Session(org.hibernate.Session)

Example 8 with Transaction

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

the class BaseCoreFunctionalTestCase method assertAllDataRemoved.

@SuppressWarnings({ "UnnecessaryBoxing", "UnnecessaryUnboxing" })
protected void assertAllDataRemoved() {
    if (!createSchema()) {
        // no tables were created...
        return;
    }
    if (!Boolean.getBoolean(VALIDATE_DATA_CLEANUP)) {
        return;
    }
    Session tmpSession = sessionFactory.openSession();
    Transaction transaction = tmpSession.beginTransaction();
    try {
        List list = tmpSession.createQuery("select o from java.lang.Object o").list();
        Map<String, Integer> items = new HashMap<String, Integer>();
        if (!list.isEmpty()) {
            for (Object element : list) {
                Integer l = items.get(tmpSession.getEntityName(element));
                if (l == null) {
                    l = 0;
                }
                l = l + 1;
                items.put(tmpSession.getEntityName(element), l);
                System.out.println("Data left: " + element);
            }
            transaction.rollback();
            fail("Data is left in the database: " + items.toString());
        }
        transaction.rollback();
    } finally {
        try {
            if (transaction.getStatus().canRollback()) {
                transaction.rollback();
            }
            tmpSession.close();
        } catch (Throwable t) {
        // intentionally empty
        }
    }
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session)

Example 9 with Transaction

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

the class TestStoreRetrieveUsingGeolatte method storeTestObjects.

private void storeTestObjects(Map<Integer, GeomEntity> stored) {
    Session session = null;
    Transaction tx = null;
    int id = -1;
    try {
        session = openSession();
        // to improve feedback in case of failure
        for (TestDataElement element : testData) {
            id = element.id;
            tx = session.beginTransaction();
            GeomEntity entity = GeomEntity.createFrom(element);
            stored.put(entity.getId(), entity);
            session.save(entity);
            tx.commit();
        }
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        throw new RuntimeException("Failed storing testsuite-suite object with id:" + id, e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}
Also used : Transaction(org.hibernate.Transaction) TestDataElement(org.hibernate.spatial.testing.TestDataElement) WktDecodeException(org.geolatte.geom.codec.WktDecodeException) Session(org.hibernate.Session)

Example 10 with Transaction

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

the class TestStoreRetrieveUsingJTS method retrieveAndCompare.

private void retrieveAndCompare(Map<Integer, GeomEntity> stored) {
    int id = -1;
    Transaction tx = null;
    Session session = null;
    try {
        session = openSession();
        tx = session.beginTransaction();
        for (GeomEntity storedEntity : stored.values()) {
            id = storedEntity.getId();
            GeomEntity retrievedEntity = (GeomEntity) session.get(GeomEntity.class, id);
            Geometry retrievedGeometry = retrievedEntity.getGeom();
            Geometry storedGeometry = storedEntity.getGeom();
            String msg = createFailureMessage(storedEntity.getId(), storedGeometry, retrievedGeometry);
            assertTrue(msg, geometryEquality.test(storedGeometry, retrievedGeometry));
        }
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        throw new RuntimeException(String.format("Failure on case: %d", id), e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) Transaction(org.hibernate.Transaction) ParseException(com.vividsolutions.jts.io.ParseException) Session(org.hibernate.Session)

Aggregations

Transaction (org.hibernate.Transaction)1272 Session (org.hibernate.Session)1250 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