Search in sources :

Example 36 with ScrollableResults

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

the class HQLScrollFetchTest method testIncompleteScrollSecondResultInTransaction.

@Test
@TestForIssue(jiraKey = "HHH-1283")
public void testIncompleteScrollSecondResultInTransaction() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    ScrollableResults results = s.createQuery(QUERY + " order by p.name asc").scroll();
    results.next();
    Parent p = (Parent) results.get(0);
    assertResultFromOneUser(p);
    results.next();
    p = (Parent) results.get(0);
    assertResultFromOneUser(p);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 37 with ScrollableResults

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

the class HQLScrollFetchTest method testIncompleteScrollLast.

@Test
@TestForIssue(jiraKey = "HHH-1283")
public void testIncompleteScrollLast() {
    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);
    results.last();
    // get the other parent entity from the persistence context.
    // since the result set was scrolled to the end, the other parent entity's collection has been
    // properly initialized.
    Parent pOther = null;
    Set childrenOther = new HashSet();
    for (Object entity : ((SessionImplementor) s).getPersistenceContext().getEntitiesByKey().values()) {
        if (Parent.class.isInstance(entity)) {
            if (entity != p) {
                if (pOther != null) {
                    fail("unexpected parent found.");
                }
                pOther = (Parent) entity;
            }
        } else if (Child.class.isInstance(entity)) {
            if (!p.getChildren().contains(entity)) {
                childrenOther.add(entity);
            }
        } else {
            fail("unexpected type of entity.");
        }
    }
    // check that the same second parent is obtained by calling Session.get()
    assertNotNull(pOther);
    assertSame(pOther, s.get(Parent.class, pOther.getId()));
    // access pOther's collection; should be completely loaded
    assertTrue(Hibernate.isInitialized(pOther.getChildren()));
    assertEquals(childrenOther, pOther.getChildren());
    assertResultFromOneUser(pOther);
    s.getTransaction().commit();
    s.close();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) HashSet(java.util.HashSet) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 38 with ScrollableResults

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

the class HQLScrollFetchTest method testIncompleteScrollFirstResultInTransaction.

@Test
public void testIncompleteScrollFirstResultInTransaction() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    ScrollableResults results = s.createQuery(QUERY + " order by p.name asc").scroll();
    results.next();
    Parent p = (Parent) results.get(0);
    assertResultFromOneUser(p);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test)

Example 39 with ScrollableResults

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

the class ScrollableCollectionFetchingTest method testScrollingJoinFetchesPositioning.

@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 testScrollingJoinFetchesPositioning() {
    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.first();
    Animal animal = (Animal) results.get(0);
    assertEquals("first() did not return expected row", data.root1Id, animal.getId());
    results.scroll(1);
    animal = (Animal) results.get(0);
    assertEquals("scroll(1) did not return expected row", data.root2Id, animal.getId());
    results.scroll(-1);
    animal = (Animal) results.get(0);
    assertEquals("scroll(-1) did not return expected row", data.root1Id, animal.getId());
    results.setRowNumber(1);
    animal = (Animal) results.get(0);
    assertEquals("setRowNumber(1) did not return expected row", data.root1Id, animal.getId());
    results.setRowNumber(2);
    animal = (Animal) results.get(0);
    assertEquals("setRowNumber(2) did not return expected row", data.root2Id, animal.getId());
    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)

Example 40 with ScrollableResults

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

the class ScrollableCollectionFetchingTest method testScrollingJoinFetchesSingleRowResultSet.

@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 testScrollingJoinFetchesSingleRowResultSet() {
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    Animal mother = new Animal();
    mother.setDescription("root-1");
    Animal daughter = new Animal();
    daughter.setDescription("daughter");
    daughter.setMother(mother);
    mother.addOffspring(daughter);
    s.save(mother);
    s.save(daughter);
    txn.commit();
    s.close();
    s = openSession();
    txn = s.beginTransaction();
    assertNotNull(s.createQuery("from Animal a left join fetch a.offspring where a.description like :desc order by a.id").setString("desc", "root%").uniqueResult());
    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();
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertFalse(results.previous());
    assertTrue(results.next());
    assertTrue(results.isFirst());
    assertTrue(results.isLast());
    assertFalse(results.next());
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertTrue(results.previous());
    assertTrue(results.isFirst());
    assertTrue(results.isLast());
    assertFalse(results.previous());
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertTrue(results.next());
    assertTrue(results.isFirst());
    assertTrue(results.isLast());
    results.beforeFirst();
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertFalse(results.previous());
    assertTrue(results.first());
    assertTrue(results.isFirst());
    assertTrue(results.isLast());
    assertFalse(results.next());
    results.afterLast();
    assertFalse(results.isFirst());
    assertFalse(results.isLast());
    assertFalse(results.next());
    assertTrue(results.last());
    assertTrue(results.isFirst());
    assertTrue(results.isLast());
    assertFalse(results.next());
    assertTrue(results.first());
    assertTrue(results.isFirst());
    assertTrue(results.isLast());
    for (int i = 1; i < 3; i++) {
        assertTrue(results.setRowNumber(1));
        assertTrue(results.isFirst());
        assertTrue(results.isLast());
        assertFalse(results.scroll(i));
        assertFalse(results.isFirst());
        assertFalse(results.isLast());
        assertTrue(results.setRowNumber(1));
        assertTrue(results.isFirst());
        assertTrue(results.isLast());
        assertFalse(results.scroll(-i));
        assertFalse(results.isFirst());
        assertFalse(results.isLast());
        if (i != 1) {
            assertFalse(results.setRowNumber(i));
            assertFalse(results.isFirst());
            assertFalse(results.isLast());
            assertFalse(results.setRowNumber(-i));
            assertFalse(results.isFirst());
            assertFalse(results.isLast());
        }
    }
    txn.commit();
    s.close();
    s = openSession();
    txn = s.beginTransaction();
    s.createQuery("delete Animal where not description like 'root%'").executeUpdate();
    s.createQuery("delete Animal").executeUpdate();
    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)

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