Search in sources :

Example 6 with ScrollableResults

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

the class CriteriaQueryTest method testScrollCriteria.

@Test
public void testScrollCriteria() {
    Session session = openSession();
    Transaction t = session.beginTransaction();
    Course course = new Course();
    course.setCourseCode("HIB");
    course.setDescription("Hibernate Training");
    session.persist(course);
    session.flush();
    session.clear();
    ScrollableResults sr = session.createCriteria(Course.class).scroll();
    assertTrue(sr.next());
    course = (Course) sr.get(0);
    assertNotNull(course);
    sr.close();
    session.delete(course);
    t.commit();
    session.close();
}
Also used : Transaction(org.hibernate.Transaction) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with ScrollableResults

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

the class ASTParserLoadingTest method testResultTransformerEntityQueries.

@Test
public void testResultTransformerEntityQueries() throws Exception {
    createTestBaseData();
    String query = "select an as an from Animal an order by bodyWeight desc";
    Session session = openSession();
    Transaction t = session.beginTransaction();
    List results = session.createQuery(query).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
    assertEquals("Incorrect result size", results.size(), 2);
    assertTrue("Incorrect return type", results.get(0) instanceof Map);
    Map map = ((Map) results.get(0));
    assertEquals(1, map.size());
    Animal firstAnimal = (Animal) map.get("an");
    map = ((Map) results.get(1));
    Animal secondAnimal = (Animal) map.get("an");
    assertEquals("Mammal #1", firstAnimal.getDescription());
    assertEquals("Mammal #2", secondAnimal.getDescription());
    assertTrue(session.contains(firstAnimal));
    assertSame(firstAnimal, session.get(Animal.class, firstAnimal.getId()));
    t.commit();
    session.close();
    session = openSession();
    t = session.beginTransaction();
    Iterator iter = session.createQuery(query).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).iterate();
    assertTrue("Incorrect result size", iter.hasNext());
    map = (Map) iter.next();
    firstAnimal = (Animal) map.get("an");
    assertEquals("Mammal #1", firstAnimal.getDescription());
    assertTrue("Incorrect result size", iter.hasNext());
    t.commit();
    session.close();
    session = openSession();
    t = session.beginTransaction();
    ScrollableResults sr = session.createQuery(query).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).scroll();
    assertTrue("Incorrect result size", sr.next());
    assertTrue("Incorrect return type", sr.get(0) instanceof Map);
    sr.close();
    t.commit();
    session.close();
    destroyTestBaseData();
}
Also used : Transaction(org.hibernate.Transaction) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test)

Example 8 with ScrollableResults

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

the class ScrollableCollectionFetchingTest method testScrollingJoinFetchesEmptyResultSet.

@Test
@SkipForDialect(value = SybaseASE15Dialect.class, jiraKey = "HHH-5229")
@SkipForDialect(value = { AbstractHANADialect.class }, comment = "HANA only supports forward-only cursors.")
public void testScrollingJoinFetchesEmptyResultSet() {
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    final String query = "from Animal a left join fetch a.offspring where a.description like :desc order by a.id";
    // first, as a control, make sure there are no results
    int size = s.createQuery(query).setString("desc", "root%").list().size();
    assertEquals(0, size);
    // now get the scrollable results
    ScrollableResults results = s.createQuery(query).setString("desc", "root%").scroll();
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertFalse(results.next());
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertFalse(results.previous());
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    results.beforeFirst();
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertFalse(results.next());
    assertFalse(results.first());
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertFalse(results.next());
    results.afterLast();
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertFalse(results.next());
    assertFalse(results.last());
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertFalse(results.next());
    for (int i = 1; i < 3; i++) {
        assertFalse(results.scroll(i));
        assertFalse(results.isFirst());
        assertFalse(results.isLast());
        assertFalse(results.scroll(-i));
        assertFalse(results.isFirst());
        assertFalse(results.isLast());
        assertFalse(results.setRowNumber(i));
        assertFalse(results.isFirst());
        assertFalse(results.isLast());
        assertFalse(results.setRowNumber(-i));
        assertFalse(results.isFirst());
        assertFalse(results.isLast());
    }
    txn.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 9 with ScrollableResults

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

the class ScrollableCollectionFetchingTest method testScrollingJoinFetchesForward.

@Test
@RequiresDialectFeature(value = DialectChecks.SupportsResultSetPositioningOnForwardOnlyCursorCheck.class, comment = "Driver does not support result set positioning  methods on forward-only cursors")
public void testScrollingJoinFetchesForward() {
    TestData data = new TestData();
    data.prepare();
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    ScrollableResults results = s.createQuery("from Animal a left join fetch a.offspring where a.description like :desc order by a.id").setString("desc", "root%").scroll(ScrollMode.FORWARD_ONLY);
    int counter = 0;
    while (results.next()) {
        counter++;
        Animal animal = (Animal) results.get(0);
        checkResult(animal);
    }
    assertEquals("unexpected result count", 2, counter);
    txn.commit();
    s.close();
    data.cleanup();
}
Also used : Transaction(org.hibernate.Transaction) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature)

Example 10 with ScrollableResults

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

the class ScrollableCollectionFetchingTest method testScrollingJoinFetchesReverse.

@Test
@SkipForDialect(value = CUBRIDDialect.class, comment = "As of verion 8.4.1 CUBRID doesn't support temporary tables. This test fails with" + "HibernateException: cannot doAfterTransactionCompletion multi-table deletes using dialect not supporting temp tables")
@SkipForDialect(value = AbstractHANADialect.class, comment = "HANA only supports forward-only cursors.")
public void testScrollingJoinFetchesReverse() {
    TestData data = new TestData();
    data.prepare();
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    ScrollableResults results = s.createQuery("from Animal a left join fetch a.offspring where a.description like :desc order by a.id").setString("desc", "root%").scroll();
    results.afterLast();
    int counter = 0;
    while (results.previous()) {
        counter++;
        Animal animal = (Animal) results.get(0);
        checkResult(animal);
    }
    assertEquals("unexpected result count", 2, counter);
    txn.commit();
    s.close();
    data.cleanup();
}
Also used : Transaction(org.hibernate.Transaction) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

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