use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class ScrollableTest method testScrollableResults.
@Test
@TestForIssue(jiraKey = "HHH-10860")
public void testScrollableResults() {
final List params = new ArrayList();
params.add(1L);
params.add(2L);
try (Session s = openSession()) {
final Query query = s.createQuery("from MyEntity e where e.id in (:ids)").setParameter("ids", params).setFetchSize(10);
try (ScrollableResults scroll = query.scroll(ScrollMode.FORWARD_ONLY)) {
int i = 0;
while (scroll.next()) {
if (i == 0) {
assertThat(((MyEntity) scroll.get()[0]).getDescription(), is("entity_1"));
} else {
assertThat(((MyEntity) scroll.get()[0]).getDescription(), is("entity_2"));
}
i++;
}
}
}
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class CriteriaCompilingTest method testDeprecation.
@Test
@TestForIssue(jiraKey = "HHH-10960")
public void testDeprecation() {
TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Order> query = builder.createQuery(Order.class);
Root<Order> from = query.from(Order.class);
query.orderBy(builder.desc(from.get("totalPrice")));
TypedQuery<Order> jpaQuery = session.createQuery(query);
org.hibernate.query.Query<?> hibQuery = jpaQuery.unwrap(org.hibernate.query.Query.class);
ScrollableResults sr = hibQuery.scroll(ScrollMode.FORWARD_ONLY);
hibQuery.setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
org.hibernate.query.Query<Order> anotherQuery = session.createQuery("select o from Order o where totalPrice in :totalPrices", Order.class);
anotherQuery.setParameterList("totalPrices", Arrays.asList(12.5d, 14.6d));
});
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class ASTParserLoadingTest method testDynamicInstantiationQueries.
@Test
public void testDynamicInstantiationQueries() throws Exception {
createTestBaseData();
Session session = openSession();
Transaction t = session.beginTransaction();
List results = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").list();
assertEquals("Incorrect result size", 2, results.size());
assertClassAssignability(results.get(0).getClass(), Animal.class);
Iterator iter = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").iterate();
assertTrue("Incorrect result size", iter.hasNext());
assertTrue("Incorrect return type", iter.next() instanceof Animal);
results = session.createQuery("select new list(an.description, an.bodyWeight) from Animal an").list();
assertEquals("Incorrect result size", 2, results.size());
assertTrue("Incorrect return type", results.get(0) instanceof List);
assertEquals("Incorrect return type", ((List) results.get(0)).size(), 2);
results = session.createQuery("select new list(an.description, an.bodyWeight) from Animal an").list();
assertEquals("Incorrect result size", 2, results.size());
assertTrue("Incorrect return type", results.get(0) instanceof List);
assertEquals("Incorrect return type", ((List) results.get(0)).size(), 2);
iter = session.createQuery("select new list(an.description, an.bodyWeight) from Animal an").iterate();
assertTrue("Incorrect result size", iter.hasNext());
Object obj = iter.next();
assertTrue("Incorrect return type", obj instanceof List);
assertEquals("Incorrect return type", ((List) obj).size(), 2);
iter = session.createQuery("select new list(an.description, an.bodyWeight) from Animal an").iterate();
assertTrue("Incorrect result size", iter.hasNext());
obj = iter.next();
assertTrue("Incorrect return type", obj instanceof List);
assertEquals("Incorrect return type", ((List) obj).size(), 2);
results = session.createQuery("select new map(an.description, an.bodyWeight) from Animal an").list();
assertEquals("Incorrect result size", 2, results.size());
assertTrue("Incorrect return type", results.get(0) instanceof Map);
assertEquals("Incorrect return type", ((Map) results.get(0)).size(), 2);
assertTrue(((Map) results.get(0)).containsKey("0"));
assertTrue(((Map) results.get(0)).containsKey("1"));
results = session.createQuery("select new map(an.description as descr, an.bodyWeight as bw) from Animal an").list();
assertEquals("Incorrect result size", 2, results.size());
assertTrue("Incorrect return type", results.get(0) instanceof Map);
assertEquals("Incorrect return type", ((Map) results.get(0)).size(), 2);
assertTrue(((Map) results.get(0)).containsKey("descr"));
assertTrue(((Map) results.get(0)).containsKey("bw"));
iter = session.createQuery("select new map(an.description, an.bodyWeight) from Animal an").iterate();
assertTrue("Incorrect result size", iter.hasNext());
obj = iter.next();
assertTrue("Incorrect return type", obj instanceof Map);
assertEquals("Incorrect return type", ((Map) obj).size(), 2);
ScrollableResults sr = session.createQuery("select new map(an.description, an.bodyWeight) from Animal an").scroll();
assertTrue("Incorrect result size", sr.next());
obj = sr.get(0);
assertTrue("Incorrect return type", obj instanceof Map);
assertEquals("Incorrect return type", ((Map) obj).size(), 2);
sr.close();
sr = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").scroll();
assertTrue("Incorrect result size", sr.next());
assertTrue("Incorrect return type", sr.get(0) instanceof Animal);
sr.close();
// caching...
QueryStatistics stats = sessionFactory().getStatistics().getQueryStatistics("select new Animal(an.description, an.bodyWeight) from Animal an");
results = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").setCacheable(true).list();
assertEquals("incorrect result size", 2, results.size());
assertClassAssignability(Animal.class, results.get(0).getClass());
long initCacheHits = stats.getCacheHitCount();
results = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").setCacheable(true).list();
assertEquals("dynamic intantiation query not served from cache", initCacheHits + 1, stats.getCacheHitCount());
assertEquals("incorrect result size", 2, results.size());
assertClassAssignability(Animal.class, results.get(0).getClass());
t.commit();
session.close();
destroyTestBaseData();
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class ASTParserLoadingTest method testResultTransformerScalarQueries.
@Test
public void testResultTransformerScalarQueries() throws Exception {
createTestBaseData();
String query = "select an.description as description, an.bodyWeight as bodyWeight from Animal an order by bodyWeight desc";
Session session = openSession();
Transaction t = session.beginTransaction();
List results = session.createQuery(query).setResultTransformer(Transformers.aliasToBean(Animal.class)).list();
assertEquals("Incorrect result size", results.size(), 2);
assertTrue("Incorrect return type", results.get(0) instanceof Animal);
Animal firstAnimal = (Animal) results.get(0);
Animal secondAnimal = (Animal) results.get(1);
assertEquals("Mammal #1", firstAnimal.getDescription());
assertEquals("Mammal #2", secondAnimal.getDescription());
assertFalse(session.contains(firstAnimal));
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
Iterator iter = session.createQuery(query).setResultTransformer(Transformers.aliasToBean(Animal.class)).iterate();
assertTrue("Incorrect result size", iter.hasNext());
assertTrue("Incorrect return type", iter.next() instanceof Animal);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
ScrollableResults sr = session.createQuery(query).setResultTransformer(Transformers.aliasToBean(Animal.class)).scroll();
assertTrue("Incorrect result size", sr.next());
assertTrue("Incorrect return type", sr.get(0) instanceof Animal);
assertFalse(session.contains(sr.get(0)));
sr.close();
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
results = session.createQuery("select a from Animal a, Animal b order by a.id").setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE).list();
assertEquals("Incorrect result size", 2, results.size());
assertTrue("Incorrect return type", results.get(0) instanceof Animal);
firstAnimal = (Animal) results.get(0);
secondAnimal = (Animal) results.get(1);
assertEquals("Mammal #1", firstAnimal.getDescription());
assertEquals("Mammal #2", secondAnimal.getDescription());
t.commit();
session.close();
destroyTestBaseData();
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class AggressiveReleaseTest method testQueryScrolling.
@Test
public void testQueryScrolling() throws Throwable {
prepare();
Session s = getSessionUnderTest();
Silly silly = new Silly("silly");
s.save(silly);
s.flush();
ScrollableResults sr = s.createQuery("from Silly").scroll();
assertTrue(sr.next());
Silly silly2 = (Silly) sr.get(0);
assertEquals(silly, silly2);
sr.close();
sr = s.createQuery("from Silly").scroll();
ScrollableResults sr2 = s.createQuery("from Silly where name = 'silly'").scroll();
assertTrue(sr.next());
assertEquals(silly, sr.get(0));
assertTrue(sr2.next());
assertEquals(silly, sr2.get(0));
sr.close();
sr2.close();
s.delete(silly);
s.flush();
release(s);
done();
}
Aggregations