Search in sources :

Example 11 with Session

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

the class HibernateCRUDTest method test_get.

public void test_get() {
    Session session = null;
    try {
        session = sessionFactory.openSession();
        doCreate(session);
        doGet(session);
    } finally {
        if (session != null) {
            session.flush();
            session.close();
        }
    }
}
Also used : Session(org.hibernate.Session)

Example 12 with Session

use of org.hibernate.Session 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 13 with Session

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

the class HibernateCRUDTest method test_delete.

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

Example 14 with Session

use of org.hibernate.Session 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 15 with Session

use of org.hibernate.Session 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)

Aggregations

Session (org.hibernate.Session)3262 Test (org.junit.Test)2414 Transaction (org.hibernate.Transaction)1255 List (java.util.List)526 ArrayList (java.util.ArrayList)363 TestForIssue (org.hibernate.testing.TestForIssue)360 Query (org.hibernate.Query)224 Test (org.testng.annotations.Test)143 Iterator (java.util.Iterator)127 Date (java.util.Date)112 Map (java.util.Map)104 BigDecimal (java.math.BigDecimal)97 Criteria (org.hibernate.Criteria)92 HashSet (java.util.HashSet)90 HibernateException (org.hibernate.HibernateException)82 HashMap (java.util.HashMap)70 SQLException (java.sql.SQLException)66 SkipForDialect (org.hibernate.testing.SkipForDialect)66 Connection (java.sql.Connection)65 Serializable (java.io.Serializable)62