Search in sources :

Example 51 with ScrollableResults

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

the class ReadOnlySessionTest method testReadOnlySessionDefaultQueryScroll.

@Test
public void testReadOnlySessionDefaultQueryScroll() {
    Session s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    Transaction t = s.beginTransaction();
    for (int i = 0; i < 100; 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);
    }
    t.commit();
    s.close();
    s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    t = s.beginTransaction();
    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);
    while (sr.next()) {
        DataPoint dp = (DataPoint) sr.get(0);
        if (++i == 50) {
            s.setReadOnly(dp, false);
        }
        dp.setDescription("done!");
    }
    t.commit();
    s.clear();
    t = s.beginTransaction();
    List single = s.createQuery("from DataPoint where description='done!'").list();
    assertEquals(1, single.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 52 with ScrollableResults

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

the class ReadOnlySessionTest method testModifiableSessionReadOnlyQueryScroll.

@Test
public void testModifiableSessionReadOnlyQueryScroll() {
    Session s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    Transaction t = s.beginTransaction();
    for (int i = 0; i < 100; 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);
    }
    t.commit();
    s.close();
    s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    t = s.beginTransaction();
    assertFalse(s.isDefaultReadOnly());
    int i = 0;
    ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc").setReadOnly(true).scroll(ScrollMode.FORWARD_ONLY);
    while (sr.next()) {
        DataPoint dp = (DataPoint) sr.get(0);
        if (++i == 50) {
            s.setReadOnly(dp, false);
        }
        dp.setDescription("done!");
    }
    t.commit();
    s.clear();
    t = s.beginTransaction();
    List single = s.createQuery("from DataPoint where description='done!'").list();
    assertEquals(1, single.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 53 with ScrollableResults

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

the class ReadOnlySessionTest method testModifiableModeWithExistingReadOnlyEntity.

@Test
public void testModifiableModeWithExistingReadOnlyEntity() {
    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(true);
    DataPoint dpLast = (DataPoint) s.get(DataPoint.class, dp.getId());
    assertTrue(s.isReadOnly(dpLast));
    int i = 0;
    ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc").setReadOnly(false).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
            assertTrue(s.isReadOnly(dp));
        } else {
            assertFalse(s.isReadOnly(dp));
        }
        if (++i == 50) {
            s.setReadOnly(dp, true);
            nExpectedChanges = (dp == dpLast ? 99 : 98);
        }
        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 54 with ScrollableResults

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

the class ReadOnlySessionTest method testModifiableSessionDefaultQueryReadOnlySessionScroll.

@Test
public void testModifiableSessionDefaultQueryReadOnlySessionScroll() {
    Session s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    Transaction t = s.beginTransaction();
    for (int i = 0; i < 100; 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);
    }
    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");
    s.setDefaultReadOnly(true);
    ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);
    s.setDefaultReadOnly(false);
    while (sr.next()) {
        DataPoint dp = (DataPoint) sr.get(0);
        if (++i == 50) {
            s.setReadOnly(dp, false);
        }
        dp.setDescription("done!");
    }
    t.commit();
    s.clear();
    t = s.beginTransaction();
    List single = s.createQuery("from DataPoint where description='done!'").list();
    assertEquals(1, single.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 55 with ScrollableResults

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

the class NativeQueryScrollableResults method testSetParameters.

@Test
public void testSetParameters() {
    final List params = new ArrayList();
    params.add(new BigInteger("2"));
    params.add(new BigInteger("3"));
    try (Session s = openSession()) {
        final Query query = s.createNativeQuery("select e.big from MY_ENTITY e where e.big in (:bigValues)").setParameter("bigValues", params);
        try (ScrollableResults scroll = query.scroll()) {
            while (scroll.next()) {
                assertThat(scroll.get()[0], not(nullValue()));
            }
        }
    }
}
Also used : Query(org.hibernate.query.Query) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) ArrayList(java.util.ArrayList) List(java.util.List) 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