Search in sources :

Example 1 with ScrollableResults

use of org.hibernate.ScrollableResults in project querydsl by querydsl.

the class IntegrationBase method scroll.

@Test
public void scroll() {
    session.save(new Cat("Bob", 10));
    session.save(new Cat("Steve", 11));
    QCat cat = QCat.cat;
    HibernateQuery<?> query = new HibernateQuery<Void>(session);
    ScrollableResults results = query.from(cat).select(cat).scroll(ScrollMode.SCROLL_INSENSITIVE);
    while (results.next()) {
        assertNotNull(results.get(0));
    }
    results.close();
}
Also used : QCat(com.querydsl.jpa.domain.QCat) HibernateQuery(com.querydsl.jpa.hibernate.HibernateQuery) Cat(com.querydsl.jpa.domain.Cat) QCat(com.querydsl.jpa.domain.QCat) ScrollableResults(org.hibernate.ScrollableResults) Test(org.junit.Test)

Example 2 with ScrollableResults

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

the class HQLTest method test_hql_api_scroll_projection_example.

@Test
public void test_hql_api_scroll_projection_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        try (ScrollableResults scrollableResults = session.createQuery("select p " + "from Person p " + "where p.name like :name").setParameter("name", "J%").scroll()) {
            while (scrollableResults.next()) {
                Person person = (Person) scrollableResults.get()[0];
                process(person);
            }
        }
    });
}
Also used : ScrollableResults(org.hibernate.ScrollableResults) Person(org.hibernate.userguide.model.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with ScrollableResults

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

the class BatchTest method withScroll.

private void withScroll() {
    withBatch();
    //tag::batch-session-scroll-example[]
    EntityManager entityManager = null;
    EntityTransaction txn = null;
    ScrollableResults scrollableResults = null;
    try {
        entityManager = entityManagerFactory().createEntityManager();
        txn = entityManager.getTransaction();
        txn.begin();
        int batchSize = 25;
        Session session = entityManager.unwrap(Session.class);
        scrollableResults = session.createQuery("select p from Person p").setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
        int count = 0;
        while (scrollableResults.next()) {
            Person Person = (Person) scrollableResults.get(0);
            processPerson(Person);
            if (++count % batchSize == 0) {
                //flush a batch of updates and release memory:
                entityManager.flush();
                entityManager.clear();
            }
        }
        txn.commit();
    } catch (RuntimeException e) {
        if (txn != null && txn.isActive())
            txn.rollback();
        throw e;
    } finally {
        if (scrollableResults != null) {
            scrollableResults.close();
        }
        if (entityManager != null) {
            entityManager.close();
        }
    }
//end::batch-session-scroll-example[]
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) ScrollableResults(org.hibernate.ScrollableResults) Person(org.hibernate.userguide.model.Person) StatelessSession(org.hibernate.StatelessSession) Session(org.hibernate.Session)

Example 4 with ScrollableResults

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

the class BatchTest method doBatchInsertUpdate.

public void doBatchInsertUpdate(int nEntities, int nBeforeFlush) {
    Session s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    Transaction t = s.beginTransaction();
    for (int i = 0; i < nEntities; i++) {
        DataPoint dp = new DataPoint();
        dp.setX(new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN));
        dp.setY(new BigDecimal(Math.cos(dp.getX().doubleValue())).setScale(19, BigDecimal.ROUND_DOWN));
        s.save(dp);
        if (i + 1 % nBeforeFlush == 0) {
            s.flush();
            s.clear();
        }
    }
    t.commit();
    s.close();
    s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    t = s.beginTransaction();
    int i = 0;
    ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc").scroll(ScrollMode.FORWARD_ONLY);
    while (sr.next()) {
        DataPoint dp = (DataPoint) sr.get(0);
        dp.setDescription("done!");
        if (++i % nBeforeFlush == 0) {
            s.flush();
            s.clear();
        }
    }
    t.commit();
    s.close();
    s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    t = s.beginTransaction();
    i = 0;
    sr = s.createQuery("from DataPoint dp order by dp.x asc").scroll(ScrollMode.FORWARD_ONLY);
    while (sr.next()) {
        DataPoint dp = (DataPoint) sr.get(0);
        s.delete(dp);
        if (++i % nBeforeFlush == 0) {
            s.flush();
            s.clear();
        }
    }
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ScrollableResults(org.hibernate.ScrollableResults) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session)

Example 5 with ScrollableResults

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

the class AggressiveReleaseTest method testSerializationFailsOnAfterStatementAggressiveReleaseWithOpenResources.

@Test
public void testSerializationFailsOnAfterStatementAggressiveReleaseWithOpenResources() throws Throwable {
    prepare();
    Session s = getSessionUnderTest();
    Silly silly = new Silly("silly");
    s.save(silly);
    // this should cause the CM to obtain a connection, and then release it
    s.flush();
    // both scroll() and iterate() cause batching to hold on
    // to resources, which should make aggressive-release not release
    // the connection (and thus cause serialization to fail)
    ScrollableResults sr = s.createQuery("from Silly").scroll();
    try {
        SerializationHelper.serialize(s);
        fail("Serialization allowed on connected session; or aggressive release released connection with open resources");
    } catch (IllegalStateException e) {
    // expected behavior
    }
    // getting the first row only because SybaseASE15Dialect throws NullPointerException
    // if data is not read beforeQuery closing the ResultSet
    sr.next();
    // Closing the ScrollableResults does currently force batching to
    // aggressively release the connection
    sr.close();
    SerializationHelper.serialize(s);
    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