use of org.hibernate.Transaction 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.Transaction 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.Transaction 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();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class JoinedSubclassFilterTest method testFiltersWithJoinedSubclass.
@Test
@SuppressWarnings({ "unchecked" })
public void testFiltersWithJoinedSubclass() {
Session s = openSession();
s.enableFilter("region").setParameter("userRegion", "US");
Transaction t = s.beginTransaction();
prepareTestData(s);
s.clear();
List 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());
Iterator 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", 2, john.getMinions().size());
break;
}
}
s.clear();
// TODO : currently impossible to define a collection-level filter w/ joined-subclass elements that will filter based on a superclass column and function correctly in (theta only?) outer joins;
// this is consistent with the behaviour of a collection-level where.
// this might be one argument for "pulling" the attached class-level filters into collection assocations,
// although we'd need some way to apply the appropriate alias in that scenario.
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", 2, 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", 2, 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();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class SubselectTest method testEntitySubselect.
@Test
@SuppressWarnings({ "unchecked" })
public void testEntitySubselect() {
Session s = openSession();
Transaction t = s.beginTransaction();
Human gavin = new Human();
gavin.setName("gavin");
gavin.setSex('M');
gavin.setAddress("Melbourne, Australia");
Alien x23y4 = new Alien();
x23y4.setIdentity("x23y4$$hu%3");
x23y4.setPlanet("Mars");
x23y4.setSpecies("martian");
s.save(gavin);
s.save(x23y4);
s.flush();
List<Being> beings = (List<Being>) s.createQuery("from Being").list();
for (Being being : beings) {
assertNotNull(being.getLocation());
assertNotNull(being.getIdentity());
assertNotNull(being.getSpecies());
}
s.clear();
sessionFactory().getCache().evictEntityRegion(Being.class);
Being gav = (Being) s.get(Being.class, gavin.getId());
assertEquals(gav.getLocation(), gavin.getAddress());
assertEquals(gav.getSpecies(), "human");
assertEquals(gav.getIdentity(), gavin.getName());
s.clear();
//test the <synchronized> tag:
gavin = (Human) s.get(Human.class, gavin.getId());
gavin.setAddress("Atlanta, GA");
gav = (Being) s.createQuery("from Being b where b.location like '%GA%'").uniqueResult();
assertEquals(gav.getLocation(), gavin.getAddress());
s.delete(gavin);
s.delete(x23y4);
assertTrue(s.createQuery("from Being").list().isEmpty());
t.commit();
s.close();
}
Aggregations