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();
}
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();
}
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);
}
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);
}
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 );
}
Aggregations