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