Search in sources :

Example 26 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 27 with ScrollableResults

use of org.hibernate.ScrollableResults in project midpoint by Evolveum.

the class SqlAuditServiceImpl method listRecordsIterativeAttempt.

private void listRecordsIterativeAttempt(String query, Map<String, Object> params, AuditResultHandler handler) {
    Session session = null;
    int count = 0;
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("List records attempt\n  query: {}\n params:\n{}", query, DebugUtil.debugDump(params, 2));
    }
    try {
        session = baseHelper.beginReadOnlyTransaction();
        Query q;
        if (StringUtils.isBlank(query)) {
            query = "from RAuditEventRecord as aer where 1=1 order by aer.timestamp desc";
            q = session.createQuery(query);
            setParametersToQuery(q, params);
        } else {
            q = session.createQuery(query);
            setParametersToQuery(q, params);
        }
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("List records attempt\n  processed query: {}", q);
        }
        ScrollableResults resultList = q.scroll();
        while (resultList.next()) {
            Object o = resultList.get(0);
            if (!(o instanceof RAuditEventRecord)) {
                throw new DtoTranslationException("Unexpected object in result set. Expected audit record, but got " + o.getClass().getSimpleName());
            }
            RAuditEventRecord raudit = (RAuditEventRecord) o;
            AuditEventRecord audit = RAuditEventRecord.fromRepo(raudit, getPrismContext());
            // TODO what if original name (in audit log) differs from the current one (in repo) ?
            audit.setInitiator(resolve(session, raudit.getInitiatorOid(), raudit.getInitiatorName(), RObjectType.USER));
            audit.setTarget(resolve(session, raudit.getTargetOid(), raudit.getTargetName(), raudit.getTargetType()));
            audit.setTargetOwner(resolve(session, raudit.getTargetOwnerOid(), raudit.getTargetOwnerName(), RObjectType.USER));
            count++;
            if (!handler.handle(audit)) {
                LOGGER.trace("Skipping handling of objects after {} was handled. ", audit);
                break;
            }
        }
        session.getTransaction().commit();
    } catch (DtoTranslationException | SchemaException ex) {
        baseHelper.handleGeneralCheckedException(ex, session, null);
    } catch (RuntimeException ex) {
        baseHelper.handleGeneralRuntimeException(ex, session, null);
    } finally {
        baseHelper.cleanupSessionAndResult(session, null);
    }
    LOGGER.trace("List records iterative attempt processed {} records", count);
}
Also used : DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) PrismObject(com.evolveum.midpoint.prism.PrismObject) ScrollableResults(org.hibernate.ScrollableResults) AuditEventRecord(com.evolveum.midpoint.audit.api.AuditEventRecord) Session(org.hibernate.Session)

Example 28 with ScrollableResults

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

the class ReadOnlySessionTest method testQueryReadOnlyScroll.

@Test
public void testQueryReadOnlyScroll() {
    Session s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    Transaction t = s.beginTransaction();
    DataPoint dp = null;
    for (int i = 0; i < 100; i++) {
        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);
    }
    t.commit();
    s.close();
    s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    t = s.beginTransaction();
    s.setDefaultReadOnly(false);
    int i = 0;
    Query query = s.createQuery("from DataPoint dp order by dp.x asc");
    assertFalse(query.isReadOnly());
    s.setDefaultReadOnly(true);
    assertTrue(query.isReadOnly());
    s.setDefaultReadOnly(false);
    assertFalse(query.isReadOnly());
    query.setReadOnly(true);
    assertTrue(query.isReadOnly());
    s.setDefaultReadOnly(true);
    assertTrue(query.isReadOnly());
    s.setDefaultReadOnly(false);
    assertTrue(query.isReadOnly());
    query.setReadOnly(false);
    assertFalse(query.isReadOnly());
    s.setDefaultReadOnly(true);
    assertFalse(query.isReadOnly());
    query.setReadOnly(true);
    assertTrue(query.isReadOnly());
    s.setDefaultReadOnly(false);
    assertFalse(s.isDefaultReadOnly());
    ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);
    assertFalse(s.isDefaultReadOnly());
    assertTrue(query.isReadOnly());
    DataPoint dpLast = (DataPoint) s.get(DataPoint.class, dp.getId());
    assertFalse(s.isReadOnly(dpLast));
    query.setReadOnly(false);
    assertFalse(query.isReadOnly());
    int nExpectedChanges = 0;
    assertFalse(s.isDefaultReadOnly());
    while (sr.next()) {
        assertFalse(s.isDefaultReadOnly());
        dp = (DataPoint) sr.get(0);
        if (dp.getId() == dpLast.getId()) {
            //dpLast existed in the session beforeQuery executing the read-only query
            assertFalse(s.isReadOnly(dp));
        } else {
            assertTrue(s.isReadOnly(dp));
        }
        if (++i == 50) {
            s.setReadOnly(dp, false);
            nExpectedChanges = (dp == dpLast ? 1 : 2);
        }
        dp.setDescription("done!");
    }
    assertFalse(s.isDefaultReadOnly());
    t.commit();
    s.clear();
    t = s.beginTransaction();
    List list = s.createQuery("from DataPoint where description='done!'").list();
    assertEquals(nExpectedChanges, list.size());
    s.createQuery("delete from DataPoint").executeUpdate();
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) List(java.util.List) ScrollableResults(org.hibernate.ScrollableResults) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 29 with ScrollableResults

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

the class ReadOnlySessionTest method testReadOnlyModeWithExistingModifiableEntity.

@Test
public void testReadOnlyModeWithExistingModifiableEntity() {
    Session s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    Transaction t = s.beginTransaction();
    DataPoint dp = null;
    for (int i = 0; i < 100; i++) {
        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);
    }
    t.commit();
    s.close();
    s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    t = s.beginTransaction();
    DataPoint dpLast = (DataPoint) s.get(DataPoint.class, dp.getId());
    assertFalse(s.isReadOnly(dpLast));
    s.setDefaultReadOnly(true);
    int i = 0;
    ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc").scroll(ScrollMode.FORWARD_ONLY);
    s.setDefaultReadOnly(false);
    int nExpectedChanges = 0;
    while (sr.next()) {
        dp = (DataPoint) sr.get(0);
        if (dp.getId() == dpLast.getId()) {
            //dpLast existed in the session beforeQuery executing the read-only query
            assertFalse(s.isReadOnly(dp));
        } else {
            assertTrue(s.isReadOnly(dp));
        }
        if (++i == 50) {
            s.setReadOnly(dp, false);
            nExpectedChanges = (dp == dpLast ? 1 : 2);
        }
        dp.setDescription("done!");
    }
    t.commit();
    s.clear();
    t = s.beginTransaction();
    List list = s.createQuery("from DataPoint where description='done!'").list();
    assertEquals(nExpectedChanges, list.size());
    s.createQuery("delete from DataPoint").executeUpdate();
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) ScrollableResults(org.hibernate.ScrollableResults) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 30 with ScrollableResults

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

the class ReadOnlyTest method testReadOnlyModeWithExistingModifiableEntity.

@Test
public void testReadOnlyModeWithExistingModifiableEntity() {
    clearCounts();
    Session s = openSession();
    Transaction t = s.beginTransaction();
    DataPoint dp = null;
    for (int i = 0; i < 100; i++) {
        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);
    }
    t.commit();
    s.close();
    assertInsertCount(100);
    assertUpdateCount(0);
    clearCounts();
    s = openSession();
    t = s.beginTransaction();
    DataPoint dpLast = (DataPoint) s.get(DataPoint.class, dp.getId());
    assertFalse(s.isReadOnly(dpLast));
    int i = 0;
    ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc").setReadOnly(true).scroll(ScrollMode.FORWARD_ONLY);
    int nExpectedChanges = 0;
    while (sr.next()) {
        dp = (DataPoint) sr.get(0);
        if (dp.getId() == dpLast.getId()) {
            //dpLast existed in the session beforeQuery executing the read-only query
            assertFalse(s.isReadOnly(dp));
        } else {
            assertTrue(s.isReadOnly(dp));
        }
        if (++i == 50) {
            s.setReadOnly(dp, false);
            nExpectedChanges = (dp == dpLast ? 1 : 2);
        }
        dp.setDescription("done!");
    }
    t.commit();
    s.clear();
    assertInsertCount(0);
    assertUpdateCount(nExpectedChanges);
    clearCounts();
    t = s.beginTransaction();
    List list = s.createQuery("from DataPoint where description='done!'").list();
    assertEquals(list.size(), nExpectedChanges);
    assertEquals(100, s.createQuery("delete from DataPoint").executeUpdate());
    t.commit();
    s.close();
    assertUpdateCount(0);
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) ScrollableResults(org.hibernate.ScrollableResults) BigDecimal(java.math.BigDecimal) 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