Search in sources :

Example 21 with Session

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

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

Example 23 with Session

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

the class TestSpatialFunctions method doInSession.

private <T> void doInSession(String hql, Map<Integer, T> result, Map<String, Object> params) {
    Session session = null;
    Transaction tx = null;
    try {
        session = openSession();
        tx = session.beginTransaction();
        Query query = session.createQuery(hql);
        setParameters(params, query);
        addQueryResults(result, query);
    } finally {
        if (tx != null) {
            tx.rollback();
        }
        if (session != null) {
            session.close();
        }
    }
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) Session(org.hibernate.Session)

Example 24 with Session

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

the class TestSpatialRestrictions method retrieveAndCompare.

private void retrieveAndCompare(Map<Integer, Boolean> dbexpected, Criterion spatialCriterion) {
    Session session = null;
    Transaction tx = null;
    try {
        session = openSession();
        tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(GeomEntity.class);
        criteria.add(spatialCriterion);
        compare(dbexpected, criteria.list());
    } finally {
        if (tx != null) {
            tx.rollback();
        }
        if (session != null) {
            session.close();
        }
    }
}
Also used : Transaction(org.hibernate.Transaction) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session)

Example 25 with Session

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

the class DiscrimSubclassFilterTest method testFiltersWithSubclass.

@Test
@SuppressWarnings({ "unchecked" })
public void testFiltersWithSubclass() {
    Session s = openSession();
    s.enableFilter("region").setParameter("userRegion", "US");
    Transaction t = s.beginTransaction();
    prepareTestData(s);
    s.clear();
    List results;
    Iterator itr;
    results = s.createQuery("from Person").list();
    assertEquals("Incorrect qry result count", 4, results.size());
    s.clear();
    results = s.createQuery("from Employee").list();
    assertEquals("Incorrect qry result count", 2, results.size());
    s.clear();
    results = new ArrayList(new HashSet(s.createQuery("from Person as p left join fetch p.minions").list()));
    assertEquals("Incorrect qry result count", 4, results.size());
    itr = results.iterator();
    while (itr.hasNext()) {
        // find john
        final Person p = (Person) itr.next();
        if (p.getName().equals("John Doe")) {
            Employee john = (Employee) p;
            assertEquals("Incorrect fecthed minions count", 1, john.getMinions().size());
            break;
        }
    }
    s.clear();
    results = new ArrayList(new HashSet(s.createQuery("from Employee as p left join fetch p.minions").list()));
    assertEquals("Incorrect qry result count", 2, results.size());
    itr = results.iterator();
    while (itr.hasNext()) {
        // find john
        final Person p = (Person) itr.next();
        if (p.getName().equals("John Doe")) {
            Employee john = (Employee) p;
            assertEquals("Incorrect fecthed minions count", 1, john.getMinions().size());
            break;
        }
    }
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    s.createQuery("delete Customer where contactOwner is not null").executeUpdate();
    s.createQuery("delete Employee where manager is not null").executeUpdate();
    s.createQuery("delete Person").executeUpdate();
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Session(org.hibernate.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

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