Search in sources :

Example 31 with ScrollableResults

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();
}
Also used : Query(org.hibernate.Query) AbstractHANADialect(org.hibernate.dialect.AbstractHANADialect) Transaction(org.hibernate.Transaction) ResultTransformer(org.hibernate.transform.ResultTransformer) List(java.util.List) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 32 with ScrollableResults

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();
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test)

Example 33 with ScrollableResults

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();
    }
}
Also used : Transaction(org.hibernate.Transaction) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test)

Example 34 with ScrollableResults

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();
}
Also used : ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test)

Example 35 with ScrollableResults

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();
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

ScrollableResults (org.hibernate.ScrollableResults)60 Session (org.hibernate.Session)56 Test (org.junit.Test)52 Transaction (org.hibernate.Transaction)35 List (java.util.List)25 BigDecimal (java.math.BigDecimal)13 ArrayList (java.util.ArrayList)13 TestForIssue (org.hibernate.testing.TestForIssue)12 Query (org.hibernate.Query)9 Iterator (java.util.Iterator)7 SkipForDialect (org.hibernate.testing.SkipForDialect)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 StatelessSession (org.hibernate.StatelessSession)3 DB2Dialect (org.hibernate.dialect.DB2Dialect)3 HSQLDialect (org.hibernate.dialect.HSQLDialect)3 SybaseDialect (org.hibernate.dialect.SybaseDialect)3 Query (org.hibernate.query.Query)3 Date (java.util.Date)2