use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class CriteriaQueryTest method testScrollCriteria.
@Test
public void testScrollCriteria() {
Session session = openSession();
Transaction t = session.beginTransaction();
Course course = new Course();
course.setCourseCode("HIB");
course.setDescription("Hibernate Training");
session.persist(course);
session.flush();
session.clear();
ScrollableResults sr = session.createCriteria(Course.class).scroll();
assertTrue(sr.next());
course = (Course) sr.get(0);
assertNotNull(course);
sr.close();
session.delete(course);
t.commit();
session.close();
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class ASTParserLoadingTest method testResultTransformerEntityQueries.
@Test
public void testResultTransformerEntityQueries() throws Exception {
createTestBaseData();
String query = "select an as an from Animal an order by bodyWeight desc";
Session session = openSession();
Transaction t = session.beginTransaction();
List results = session.createQuery(query).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
assertEquals("Incorrect result size", results.size(), 2);
assertTrue("Incorrect return type", results.get(0) instanceof Map);
Map map = ((Map) results.get(0));
assertEquals(1, map.size());
Animal firstAnimal = (Animal) map.get("an");
map = ((Map) results.get(1));
Animal secondAnimal = (Animal) map.get("an");
assertEquals("Mammal #1", firstAnimal.getDescription());
assertEquals("Mammal #2", secondAnimal.getDescription());
assertTrue(session.contains(firstAnimal));
assertSame(firstAnimal, session.get(Animal.class, firstAnimal.getId()));
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
Iterator iter = session.createQuery(query).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).iterate();
assertTrue("Incorrect result size", iter.hasNext());
map = (Map) iter.next();
firstAnimal = (Animal) map.get("an");
assertEquals("Mammal #1", firstAnimal.getDescription());
assertTrue("Incorrect result size", iter.hasNext());
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
ScrollableResults sr = session.createQuery(query).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).scroll();
assertTrue("Incorrect result size", sr.next());
assertTrue("Incorrect return type", sr.get(0) instanceof Map);
sr.close();
t.commit();
session.close();
destroyTestBaseData();
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class ScrollableCollectionFetchingTest method testScrollingJoinFetchesEmptyResultSet.
@Test
@SkipForDialect(value = SybaseASE15Dialect.class, jiraKey = "HHH-5229")
@SkipForDialect(value = { AbstractHANADialect.class }, comment = "HANA only supports forward-only cursors.")
public void testScrollingJoinFetchesEmptyResultSet() {
Session s = openSession();
Transaction txn = s.beginTransaction();
final String query = "from Animal a left join fetch a.offspring where a.description like :desc order by a.id";
// first, as a control, make sure there are no results
int size = s.createQuery(query).setString("desc", "root%").list().size();
assertEquals(0, size);
// now get the scrollable results
ScrollableResults results = s.createQuery(query).setString("desc", "root%").scroll();
assertFalse(results.isFirst());
assertFalse(results.isLast());
assertFalse(results.next());
assertFalse(results.isFirst());
assertFalse(results.isLast());
assertFalse(results.previous());
assertFalse(results.isFirst());
assertFalse(results.isLast());
results.beforeFirst();
assertFalse(results.isFirst());
assertFalse(results.isLast());
assertFalse(results.next());
assertFalse(results.first());
assertFalse(results.isFirst());
assertFalse(results.isLast());
assertFalse(results.next());
results.afterLast();
assertFalse(results.isFirst());
assertFalse(results.isLast());
assertFalse(results.next());
assertFalse(results.last());
assertFalse(results.isFirst());
assertFalse(results.isLast());
assertFalse(results.next());
for (int i = 1; i < 3; i++) {
assertFalse(results.scroll(i));
assertFalse(results.isFirst());
assertFalse(results.isLast());
assertFalse(results.scroll(-i));
assertFalse(results.isFirst());
assertFalse(results.isLast());
assertFalse(results.setRowNumber(i));
assertFalse(results.isFirst());
assertFalse(results.isLast());
assertFalse(results.setRowNumber(-i));
assertFalse(results.isFirst());
assertFalse(results.isLast());
}
txn.commit();
s.close();
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class ScrollableCollectionFetchingTest method testScrollingJoinFetchesForward.
@Test
@RequiresDialectFeature(value = DialectChecks.SupportsResultSetPositioningOnForwardOnlyCursorCheck.class, comment = "Driver does not support result set positioning methods on forward-only cursors")
public void testScrollingJoinFetchesForward() {
TestData data = new TestData();
data.prepare();
Session s = openSession();
Transaction txn = s.beginTransaction();
ScrollableResults results = s.createQuery("from Animal a left join fetch a.offspring where a.description like :desc order by a.id").setString("desc", "root%").scroll(ScrollMode.FORWARD_ONLY);
int counter = 0;
while (results.next()) {
counter++;
Animal animal = (Animal) results.get(0);
checkResult(animal);
}
assertEquals("unexpected result count", 2, counter);
txn.commit();
s.close();
data.cleanup();
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class ScrollableCollectionFetchingTest method testScrollingJoinFetchesReverse.
@Test
@SkipForDialect(value = CUBRIDDialect.class, comment = "As of verion 8.4.1 CUBRID doesn't support temporary tables. This test fails with" + "HibernateException: cannot doAfterTransactionCompletion multi-table deletes using dialect not supporting temp tables")
@SkipForDialect(value = AbstractHANADialect.class, comment = "HANA only supports forward-only cursors.")
public void testScrollingJoinFetchesReverse() {
TestData data = new TestData();
data.prepare();
Session s = openSession();
Transaction txn = s.beginTransaction();
ScrollableResults results = s.createQuery("from Animal a left join fetch a.offspring where a.description like :desc order by a.id").setString("desc", "root%").scroll();
results.afterLast();
int counter = 0;
while (results.previous()) {
counter++;
Animal animal = (Animal) results.get(0);
checkResult(animal);
}
assertEquals("unexpected result count", 2, counter);
txn.commit();
s.close();
data.cleanup();
}
Aggregations