use of org.hibernate.query.criteria.JpaSelection in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testJoinWithFetchJoinWithAliasedJoinedAndOwnerProjectedList.
@Test
public void testJoinWithFetchJoinWithAliasedJoinedAndOwnerProjectedList(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);
JpaRoot<Student> root = criteria.from(Student.class);
final JpaSelection<Student> st = root.alias("s");
root.fetch("enrolments", JoinType.LEFT);
final Selection<Object> pc = root.join("preferredCourse", JoinType.LEFT).alias("pc");
criteria.orderBy(builder.asc(root.get("studentNumber")));
criteria.multiselect(pc, st);
return criteria;
}
};
HqlExecutor hqlSelectNewMapExecutor = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select pc, s from Student s left join fetch s.enrolments left join s.preferredCourse pc order by s.studentNumber");
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
Object[] yogiObjects = (Object[]) resultList.get(0);
assertEquals(yogiExpected, yogiObjects[1]);
assertEquals(yogiExpected.getPreferredCourse().getCourseCode(), ((Course) yogiObjects[0]).getCourseCode());
Object[] shermanObjects = (Object[]) resultList.get(1);
assertEquals(shermanExpected, shermanObjects[1]);
assertNull(shermanObjects[0]);
if (areDynamicNonLazyAssociationsChecked()) {
assertEquals(yogiExpected.getPreferredCourse(), yogiObjects[0]);
assertTrue(Hibernate.isInitialized(((Student) yogiObjects[1]).getEnrolments()));
assertEquals(yogiExpected.getEnrolments(), ((Student) yogiObjects[1]).getEnrolments());
assertTrue(Hibernate.isInitialized(((Student) shermanObjects[1]).getEnrolments()));
assertEquals(shermanExpected.getEnrolments(), (((Student) shermanObjects[1]).getEnrolments()));
}
};
runTest(hqlSelectNewMapExecutor, criteriaExecutor, checker, false, scope);
}
Aggregations