use of org.hibernate.query.criteria.JpaCriteriaQuery in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testEntityWithSelectFetchedLazyOneToManySingleElementListCriteria.
@Test
public void testEntityWithSelectFetchedLazyOneToManySingleElementListCriteria(SessionFactoryScope scope) throws Exception {
CriteriaExecutor criteriaExecutorUnaliased = new CriteriaExecutor() {
@Override
protected ResultTransformer getResultTransformer() {
return null;
}
@Override
protected JpaCriteriaQuery getCriteria(Session s) {
CriteriaBuilder builder = s.getCriteriaBuilder();
JpaCriteriaQuery<Student> criteria = (JpaCriteriaQuery<Student>) builder.createQuery(Student.class);
JpaRoot<Student> root = criteria.from(Student.class);
root.join("enrolments");
criteria.orderBy(builder.asc(root.get("studentNumber")));
return criteria;
// return s.createCriteria( Student.class, "s" )
// .setFetchMode( "enrolments", FetchMode.SELECT )
// .addOrder( Order.asc( "s.studentNumber" ) );
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
assertEquals(yogiExpected, resultList.get(0));
assertEquals(shermanExpected, resultList.get(1));
assertNotNull(((Student) resultList.get(0)).getEnrolments());
assertFalse(Hibernate.isInitialized(((Student) resultList.get(0)).getEnrolments()));
assertNotNull(((Student) resultList.get(1)).getEnrolments());
assertFalse(Hibernate.isInitialized(((Student) resultList.get(1)).getEnrolments()));
};
runTest(null, criteriaExecutorUnaliased, checker, false, scope);
}
use of org.hibernate.query.criteria.JpaCriteriaQuery in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testEntityWithJoinedLazyOneToManySingleElementList.
@Test
public void testEntityWithJoinedLazyOneToManySingleElementList(SessionFactoryScope scope) throws Exception {
CriteriaExecutor criteriaExecutorUnaliased = new CriteriaExecutor() {
@Override
protected ResultTransformer getResultTransformer() {
return null;
}
@Override
protected JpaCriteriaQuery getCriteria(Session s) {
CriteriaBuilder builder = s.getCriteriaBuilder();
JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery(Object[].class);
Root<Student> root = criteria.from(Student.class);
root.join("enrolments", JoinType.LEFT);
criteria.multiselect(root, root.get("enrolments"));
criteria.orderBy(builder.asc(root.get("studentNumber")));
return criteria;
}
};
HqlExecutor hqlExecutorUnaliased = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select s, s.enrolments from Student s left join s.enrolments order by s.studentNumber");
}
};
HqlExecutor hqlExecutorAliased = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select s, e from Student s left join s.enrolments e order by s.studentNumber");
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
assertTrue(resultList.get(0) instanceof Object[]);
Object[] yogiObjects = (Object[]) resultList.get(0);
assertEquals(yogiExpected, yogiObjects[0]);
assertEquals(yogiEnrolmentExpected, yogiObjects[1]);
assertTrue(resultList.get(0) instanceof Object[]);
Object[] shermanObjects = (Object[]) resultList.get(1);
assertEquals(shermanExpected, shermanObjects[0]);
assertEquals(shermanEnrolmentExpected, shermanObjects[1]);
};
runTest(hqlExecutorUnaliased, criteriaExecutorUnaliased, checker, false, scope);
runTest(hqlExecutorAliased, null, checker, false, scope);
}
use of org.hibernate.query.criteria.JpaCriteriaQuery in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testEntityWithJoinedLazyManyToOneList.
@Test
public void testEntityWithJoinedLazyManyToOneList(SessionFactoryScope scope) throws Exception {
CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {
@Override
protected ResultTransformer getResultTransformer() {
return null;
}
@Override
protected JpaCriteriaQuery getCriteria(Session s) {
CriteriaBuilder builder = s.getCriteriaBuilder();
JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery(Object[].class);
Root<Student> root = criteria.from(Student.class);
Join<Object, Object> preferredCourse = root.join("preferredCourse", JoinType.LEFT);
criteria.multiselect(root, preferredCourse);
criteria.orderBy(builder.asc(root.get("studentNumber")));
return criteria;
}
};
HqlExecutor hqlExecutorAliased = new HqlExecutor() {
@Override
protected Query getQuery(Session s) {
return s.createQuery("select s, p from Student s left join s.preferredCourse p order by s.studentNumber");
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
Object[] yogiObjects = (Object[]) resultList.get(0);
assertEquals(yogiExpected, yogiObjects[0]);
assertEquals(yogiExpected.getPreferredCourse(), yogiObjects[1]);
Object[] shermanObjects = (Object[]) resultList.get(1);
assertEquals(shermanExpected, shermanObjects[0]);
assertNull(shermanObjects[1]);
};
runTest(hqlExecutorAliased, criteriaExecutor, checker, false, scope);
}
use of org.hibernate.query.criteria.JpaCriteriaQuery in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testMultiSelectNewMapUsingAliasesList.
@Test
public void testMultiSelectNewMapUsingAliasesList(SessionFactoryScope scope) throws Exception {
CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {
@Override
protected JpaCriteriaQuery getCriteria(Session s) {
CriteriaBuilder builder = s.getCriteriaBuilder();
JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery();
Root<Student> root = criteria.from(Student.class);
criteria.multiselect(root.get("studentNumber").alias("sNumber"), root.get("name").alias("sName"));
criteria.orderBy(builder.asc(root.get("studentNumber")));
return criteria;
// return s.createCriteria( Student.class, "s" )
// .setProjection(
// Projections.projectionList()
// .add( Property.forName( "s.studentNumber" ).as( "sNumber" ) )
// .add( Property.forName( "s.name" ).as( "sName" ) )
// )
// .addOrder( Order.asc( "s.studentNumber" ) )
// .setResultTransformer( Transformers.ALIAS_TO_ENTITY_MAP );
}
};
HqlExecutor hqlExecutor = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select new map(s.studentNumber as sNumber, s.name as sName) from Student s order by s.studentNumber");
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
Map yogiMap = (Map) resultList.get(0);
assertEquals(yogiExpected.getStudentNumber(), yogiMap.get("sNumber"));
assertEquals(yogiExpected.getName(), yogiMap.get("sName"));
Map shermanMap = (Map) resultList.get(1);
assertEquals(shermanExpected.getStudentNumber(), shermanMap.get("sNumber"));
assertEquals(shermanExpected.getName(), shermanMap.get("sName"));
};
runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
use of org.hibernate.query.criteria.JpaCriteriaQuery in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testMultiAggregatedPropProjectionSingleResult.
@Test
public void testMultiAggregatedPropProjectionSingleResult(SessionFactoryScope scope) throws Exception {
CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {
@Override
protected ResultTransformer getResultTransformer() {
return null;
}
@Override
protected JpaCriteriaQuery getCriteria(Session s) {
CriteriaBuilder builder = s.getCriteriaBuilder();
JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery(Object[].class);
Root<Enrolment> root = criteria.from(Enrolment.class);
criteria.multiselect(builder.min(root.get("studentNumber")), builder.max(root.get("studentNumber")));
return criteria;
// return s.createCriteria( Enrolment.class )
// .setProjection(
// Projections.projectionList()
// .add( Projections.min( "studentNumber" ).as( "minStudentNumber" ) )
// .add( Projections.max( "studentNumber" ).as( "maxStudentNumber" ) )
// );
}
};
HqlExecutor hqlExecutor = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select min( e.studentNumber ) as minStudentNumber, max( e.studentNumber ) as maxStudentNumber from Enrolment e");
}
};
ResultChecker checker = results -> {
assertTrue(results instanceof Object[]);
Object[] resultObjects = (Object[]) results;
assertEquals(yogiExpected.getStudentNumber(), resultObjects[0]);
assertEquals(shermanExpected.getStudentNumber(), resultObjects[1]);
};
runTest(hqlExecutor, criteriaExecutor, checker, true, scope);
}
Aggregations