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