use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class ResultTransformerTest method testResultTransformerIsAppliedToScrollableResults.
@Test
@TestForIssue(jiraKey = "HHH-3694")
public void testResultTransformerIsAppliedToScrollableResults() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
PartnerA a = new PartnerA();
a.setName("Partner A");
PartnerB b = new PartnerB();
b.setName("Partner B");
Contract obj1 = new Contract();
obj1.setName("Contract");
obj1.setA(a);
obj1.setB(b);
s.save(a);
s.save(b);
s.save(obj1);
tx.commit();
s.close();
s = openSession();
Query q = s.getNamedQuery(Contract.class.getName() + ".testQuery");
q.setFetchSize(100);
q.setResultTransformer(new ResultTransformer() {
private static final long serialVersionUID = -5815434828170704822L;
public Object transformTuple(Object[] arg0, String[] arg1) {
// return only the PartnerA object from the query
return arg0[1];
}
@SuppressWarnings("unchecked")
public List transformList(List arg0) {
return arg0;
}
});
ScrollableResults sr = q.scroll();
// does not support java.sql.ResultSet.first()
if (getDialect() instanceof AbstractHANADialect) {
sr.next();
} else {
sr.first();
}
Object[] row = sr.get();
assertEquals(1, row.length);
Object obj = row[0];
assertTrue(obj instanceof PartnerA);
PartnerA obj2 = (PartnerA) obj;
assertEquals("Partner A", obj2.getName());
s.close();
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class CMTTest method testCurrentSessionWithScroll.
@Test
public void testCurrentSessionWithScroll() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = sessionFactory().getCurrentSession();
Map item1 = new HashMap();
item1.put("name", "Item - 1");
item1.put("description", "The first item");
s.persist("Item", item1);
Map item2 = new HashMap();
item2.put("name", "Item - 2");
item2.put("description", "The second item");
s.persist("Item", item2);
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
// First, test partially scrolling the result with out closing
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = sessionFactory().getCurrentSession();
ScrollableResults results = s.createQuery("from Item").scroll();
results.next();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
// Next, test partially scrolling the result with closing
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = sessionFactory().getCurrentSession();
results = s.createQuery("from Item").scroll();
results.next();
results.close();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
// Next, scroll the entire result (w/o closing)
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = sessionFactory().getCurrentSession();
results = s.createQuery("from Item").scroll();
while (results.next()) {
// do nothing
}
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
// Next, scroll the entire result (closing)
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = sessionFactory().getCurrentSession();
results = s.createQuery("from Item").scroll();
while (results.next()) {
// do nothing
}
results.close();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = sessionFactory().getCurrentSession();
s.createQuery("delete from Item").executeUpdate();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class HQLScrollFetchTest method testScrollOrderChildrenDesc.
@Test
public void testScrollOrderChildrenDesc() {
Session s = openSession();
Transaction t = s.beginTransaction();
Parent p0 = new Parent("parent0");
s.save(p0);
t.commit();
s.close();
s = openSession();
ScrollableResults results = s.createQuery(QUERY + " order by c.name desc").scroll();
List list = new ArrayList();
while (results.next()) {
list.add(results.get(0));
}
try {
assertResultFromAllUsers(list);
fail("should have failed because data is ordered incorrectly.");
} catch (AssertionError ex) {
// expected
} finally {
s.close();
}
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class HQLScrollFetchTest method testIncompleteScrollFirstResult.
@Test
public void testIncompleteScrollFirstResult() {
Session s = openSession();
s.beginTransaction();
ScrollableResults results = s.createQuery(QUERY + " order by p.name asc").scroll();
results.next();
Parent p = (Parent) results.get(0);
assertResultFromOneUser(p);
s.getTransaction().commit();
s.close();
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class HQLScrollFetchTest method testScrollOrderParentDesc.
@Test
@TestForIssue(jiraKey = "HHH-1283")
public void testScrollOrderParentDesc() {
Session s = openSession();
s.beginTransaction();
ScrollableResults results = s.createQuery(QUERY + " order by p.name desc").scroll();
List list = new ArrayList();
while (results.next()) {
list.add(results.get(0));
}
assertResultFromAllUsers(list);
s.getTransaction().commit();
s.close();
}
Aggregations