Search in sources :

Example 41 with ScrollableResults

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

the class ScrollableTest method testScrollableResults.

@Test
@TestForIssue(jiraKey = "HHH-10860")
public void testScrollableResults() {
    final List params = new ArrayList();
    params.add(1L);
    params.add(2L);
    try (Session s = openSession()) {
        final Query query = s.createQuery("from MyEntity e where e.id in (:ids)").setParameter("ids", params).setFetchSize(10);
        try (ScrollableResults scroll = query.scroll(ScrollMode.FORWARD_ONLY)) {
            int i = 0;
            while (scroll.next()) {
                if (i == 0) {
                    assertThat(((MyEntity) scroll.get()[0]).getDescription(), is("entity_1"));
                } else {
                    assertThat(((MyEntity) scroll.get()[0]).getDescription(), is("entity_2"));
                }
                i++;
            }
        }
    }
}
Also used : Query(org.hibernate.query.Query) 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)

Example 42 with ScrollableResults

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

the class CriteriaCompilingTest method testDeprecation.

@Test
@TestForIssue(jiraKey = "HHH-10960")
public void testDeprecation() {
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        CriteriaBuilder builder = session.getCriteriaBuilder();
        CriteriaQuery<Order> query = builder.createQuery(Order.class);
        Root<Order> from = query.from(Order.class);
        query.orderBy(builder.desc(from.get("totalPrice")));
        TypedQuery<Order> jpaQuery = session.createQuery(query);
        org.hibernate.query.Query<?> hibQuery = jpaQuery.unwrap(org.hibernate.query.Query.class);
        ScrollableResults sr = hibQuery.scroll(ScrollMode.FORWARD_ONLY);
        hibQuery.setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
        org.hibernate.query.Query<Order> anotherQuery = session.createQuery("select o from Order o where totalPrice in :totalPrices", Order.class);
        anotherQuery.setParameterList("totalPrices", Arrays.asList(12.5d, 14.6d));
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 43 with ScrollableResults

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

the class ASTParserLoadingTest method testDynamicInstantiationQueries.

@Test
public void testDynamicInstantiationQueries() throws Exception {
    createTestBaseData();
    Session session = openSession();
    Transaction t = session.beginTransaction();
    List results = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").list();
    assertEquals("Incorrect result size", 2, results.size());
    assertClassAssignability(results.get(0).getClass(), Animal.class);
    Iterator iter = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").iterate();
    assertTrue("Incorrect result size", iter.hasNext());
    assertTrue("Incorrect return type", iter.next() instanceof Animal);
    results = session.createQuery("select new list(an.description, an.bodyWeight) from Animal an").list();
    assertEquals("Incorrect result size", 2, results.size());
    assertTrue("Incorrect return type", results.get(0) instanceof List);
    assertEquals("Incorrect return type", ((List) results.get(0)).size(), 2);
    results = session.createQuery("select new list(an.description, an.bodyWeight) from Animal an").list();
    assertEquals("Incorrect result size", 2, results.size());
    assertTrue("Incorrect return type", results.get(0) instanceof List);
    assertEquals("Incorrect return type", ((List) results.get(0)).size(), 2);
    iter = session.createQuery("select new list(an.description, an.bodyWeight) from Animal an").iterate();
    assertTrue("Incorrect result size", iter.hasNext());
    Object obj = iter.next();
    assertTrue("Incorrect return type", obj instanceof List);
    assertEquals("Incorrect return type", ((List) obj).size(), 2);
    iter = session.createQuery("select new list(an.description, an.bodyWeight) from Animal an").iterate();
    assertTrue("Incorrect result size", iter.hasNext());
    obj = iter.next();
    assertTrue("Incorrect return type", obj instanceof List);
    assertEquals("Incorrect return type", ((List) obj).size(), 2);
    results = session.createQuery("select new map(an.description, an.bodyWeight) from Animal an").list();
    assertEquals("Incorrect result size", 2, results.size());
    assertTrue("Incorrect return type", results.get(0) instanceof Map);
    assertEquals("Incorrect return type", ((Map) results.get(0)).size(), 2);
    assertTrue(((Map) results.get(0)).containsKey("0"));
    assertTrue(((Map) results.get(0)).containsKey("1"));
    results = session.createQuery("select new map(an.description as descr, an.bodyWeight as bw) from Animal an").list();
    assertEquals("Incorrect result size", 2, results.size());
    assertTrue("Incorrect return type", results.get(0) instanceof Map);
    assertEquals("Incorrect return type", ((Map) results.get(0)).size(), 2);
    assertTrue(((Map) results.get(0)).containsKey("descr"));
    assertTrue(((Map) results.get(0)).containsKey("bw"));
    iter = session.createQuery("select new map(an.description, an.bodyWeight) from Animal an").iterate();
    assertTrue("Incorrect result size", iter.hasNext());
    obj = iter.next();
    assertTrue("Incorrect return type", obj instanceof Map);
    assertEquals("Incorrect return type", ((Map) obj).size(), 2);
    ScrollableResults sr = session.createQuery("select new map(an.description, an.bodyWeight) from Animal an").scroll();
    assertTrue("Incorrect result size", sr.next());
    obj = sr.get(0);
    assertTrue("Incorrect return type", obj instanceof Map);
    assertEquals("Incorrect return type", ((Map) obj).size(), 2);
    sr.close();
    sr = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").scroll();
    assertTrue("Incorrect result size", sr.next());
    assertTrue("Incorrect return type", sr.get(0) instanceof Animal);
    sr.close();
    // caching...
    QueryStatistics stats = sessionFactory().getStatistics().getQueryStatistics("select new Animal(an.description, an.bodyWeight) from Animal an");
    results = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").setCacheable(true).list();
    assertEquals("incorrect result size", 2, results.size());
    assertClassAssignability(Animal.class, results.get(0).getClass());
    long initCacheHits = stats.getCacheHitCount();
    results = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").setCacheable(true).list();
    assertEquals("dynamic intantiation query not served from cache", initCacheHits + 1, stats.getCacheHitCount());
    assertEquals("incorrect result size", 2, results.size());
    assertClassAssignability(Animal.class, results.get(0).getClass());
    t.commit();
    session.close();
    destroyTestBaseData();
}
Also used : Transaction(org.hibernate.Transaction) QueryStatistics(org.hibernate.stat.QueryStatistics) 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 44 with ScrollableResults

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

the class ASTParserLoadingTest method testResultTransformerScalarQueries.

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

Example 45 with ScrollableResults

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

the class AggressiveReleaseTest method testQueryScrolling.

@Test
public void testQueryScrolling() throws Throwable {
    prepare();
    Session s = getSessionUnderTest();
    Silly silly = new Silly("silly");
    s.save(silly);
    s.flush();
    ScrollableResults sr = s.createQuery("from Silly").scroll();
    assertTrue(sr.next());
    Silly silly2 = (Silly) sr.get(0);
    assertEquals(silly, silly2);
    sr.close();
    sr = s.createQuery("from Silly").scroll();
    ScrollableResults sr2 = s.createQuery("from Silly where name = 'silly'").scroll();
    assertTrue(sr.next());
    assertEquals(silly, sr.get(0));
    assertTrue(sr2.next());
    assertEquals(silly, sr2.get(0));
    sr.close();
    sr2.close();
    s.delete(silly);
    s.flush();
    release(s);
    done();
}
Also used : ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) 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