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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
Aggregations