Search in sources :

Example 21 with ScrollableResults

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

the class ReadOnlySessionTest method testQueryModifiableScroll.

@Test
public void testQueryModifiableScroll() {
    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);
    int i = 0;
    Query query = s.createQuery("from DataPoint dp order by dp.x asc");
    assertTrue(query.isReadOnly());
    s.setDefaultReadOnly(false);
    assertFalse(query.isReadOnly());
    s.setDefaultReadOnly(true);
    assertTrue(query.isReadOnly());
    query.setReadOnly(false);
    assertFalse(query.isReadOnly());
    s.setDefaultReadOnly(false);
    assertFalse(query.isReadOnly());
    s.setDefaultReadOnly(true);
    assertFalse(query.isReadOnly());
    query.setReadOnly(true);
    assertTrue(query.isReadOnly());
    s.setDefaultReadOnly(false);
    assertTrue(query.isReadOnly());
    query.setReadOnly(false);
    assertFalse(query.isReadOnly());
    s.setDefaultReadOnly(true);
    assertTrue(s.isDefaultReadOnly());
    ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);
    assertFalse(query.isReadOnly());
    DataPoint dpLast = (DataPoint) s.get(DataPoint.class, dp.getId());
    assertTrue(s.isReadOnly(dpLast));
    query.setReadOnly(true);
    assertTrue(query.isReadOnly());
    int nExpectedChanges = 0;
    assertTrue(s.isDefaultReadOnly());
    while (sr.next()) {
        assertTrue(s.isDefaultReadOnly());
        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!");
    }
    assertTrue(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 22 with ScrollableResults

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

the class ReadOnlySessionTest method testReadOnlySessionModifiableQueryScroll.

@Test
public void testReadOnlySessionModifiableQueryScroll() {
    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").setReadOnly(false).scroll(ScrollMode.FORWARD_ONLY);
    while (sr.next()) {
        DataPoint dp = (DataPoint) sr.get(0);
        if (++i == 50) {
            s.setReadOnly(dp, true);
        }
        dp.setDescription("done!");
    }
    t.commit();
    s.clear();
    t = s.beginTransaction();
    List list = s.createQuery("from DataPoint where description='done!'").list();
    assertEquals(99, 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 23 with ScrollableResults

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

the class ReadOnlyTest method testModifiableModeWithExistingReadOnlyEntity.

@Test
public void testModifiableModeWithExistingReadOnlyEntity() {
    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));
    s.setReadOnly(dpLast, true);
    assertTrue(s.isReadOnly(dpLast));
    dpLast.setDescription("oy");
    int i = 0;
    assertUpdateCount(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();
    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)

Example 24 with ScrollableResults

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

the class ReadOnlyTest method testReadOnlyModeAutoFlushOnQuery.

@Test
public void testReadOnlyModeAutoFlushOnQuery() {
    clearCounts();
    Session s = openSession();
    Transaction t = s.beginTransaction();
    DataPoint dpFirst = null;
    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);
    }
    assertInsertCount(0);
    assertUpdateCount(0);
    ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc").setReadOnly(true).scroll(ScrollMode.FORWARD_ONLY);
    assertInsertCount(100);
    assertUpdateCount(0);
    clearCounts();
    while (sr.next()) {
        DataPoint dp = (DataPoint) sr.get(0);
        assertFalse(s.isReadOnly(dp));
        s.delete(dp);
    }
    t.commit();
    s.close();
    assertUpdateCount(0);
    assertDeleteCount(100);
}
Also used : Transaction(org.hibernate.Transaction) ScrollableResults(org.hibernate.ScrollableResults) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 25 with ScrollableResults

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

the class ReadOnlyTest method testReadOnlyMode.

@Test
public void testReadOnlyMode() {
    clearCounts();
    Session s = openSession();
    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();
    assertInsertCount(100);
    assertUpdateCount(0);
    clearCounts();
    s = openSession();
    t = s.beginTransaction();
    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();
    assertUpdateCount(1);
    clearCounts();
    s.clear();
    t = s.beginTransaction();
    List single = s.createQuery("from DataPoint where description='done!'").list();
    assertEquals(single.size(), 1);
    assertEquals(100, s.createQuery("delete from DataPoint").executeUpdate());
    t.commit();
    s.close();
    assertUpdateCount(0);
//deletes from Query.executeUpdate() are not tracked
//assertDeleteCount( 100 );
}
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