use of org.hibernate.query.criteria.JpaRoot in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testEntityWithJoinFetchedLazyManyToOneList.
@Test
public void testEntityWithJoinFetchedLazyManyToOneList(SessionFactoryScope scope) throws Exception {
// unaliased
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.fetch("preferredCourse", JoinType.LEFT);
criteria.orderBy(builder.asc(root.get("studentNumber")));
return criteria;
// return s.createCriteria( Student.class, "s" )
// .setFetchMode( "preferredCourse", FetchMode.JOIN )
// .addOrder( Order.asc( "s.studentNumber") );
}
};
HqlExecutor hqlExecutorUnaliased = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("from Student s left join fetch s.preferredCourse order by s.studentNumber");
}
};
HqlExecutor hqlExecutorAliased = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("from Student s left join fetch s.preferredCourse pCourse order by s.studentNumber");
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
assertEquals(yogiExpected, resultList.get(0));
assertEquals(shermanExpected, resultList.get(1));
assertEquals(yogiExpected.getPreferredCourse().getCourseCode(), ((Student) resultList.get(0)).getPreferredCourse().getCourseCode());
assertNull(((Student) resultList.get(1)).getPreferredCourse());
};
runTest(hqlExecutorUnaliased, criteriaExecutorUnaliased, checker, false, scope);
runTest(hqlExecutorAliased, null, checker, false, scope);
}
use of org.hibernate.query.criteria.JpaRoot in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testAliasToEntityMapMultiProjectionList.
@Test
public void testAliasToEntityMapMultiProjectionList(SessionFactoryScope scope) throws Exception {
CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {
@Override
protected JpaCriteriaQuery getCriteria(Session s) {
CriteriaBuilder builder = s.getCriteriaBuilder();
JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery();
final JpaRoot<Enrolment> root = criteria.from(Enrolment.class);
criteria.multiselect(root.get("student").alias("student"), root.get("semester").alias("semester"), root.get("year").alias("year"), root.get("course").alias("course"));
criteria.orderBy(builder.asc(root.get("studentNumber")));
return criteria;
// return s.createCriteria( Enrolment.class, "e" )
// .setProjection(
// Projections.projectionList()
// .add( Property.forName( "e.student" ), "student" )
// .add( Property.forName( "e.semester" ), "semester" )
// .add( Property.forName( "e.year" ), "year" )
// .add( Property.forName( "e.course" ), "course" )
// )
// .addOrder( Order.asc( "studentNumber") )
// .setResultTransformer( Transformers.ALIAS_TO_ENTITY_MAP );
}
};
HqlExecutor hqlExecutor = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select e.student as student, e.semester as semester, e.year as year, e.course as course from Enrolment e order by e.studentNumber").setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
Map yogiMap = (Map) resultList.get(0);
Map shermanMap = (Map) resultList.get(1);
assertEquals(4, yogiMap.size());
assertEquals(4, shermanMap.size());
assertTrue(yogiMap.get("student") instanceof Student);
assertTrue(shermanMap.get("student") instanceof Student);
// TODO: following are initialized for hql and uninitialied for criteria; why?
// assertFalse( Hibernate.isInitialized( yogiMap.get( "student" ) ) );
// assertFalse( Hibernate.isInitialized( shermanMap.get( "student" ) ) );
assertEquals(yogiExpected.getStudentNumber(), ((Student) yogiMap.get("student")).getStudentNumber());
assertEquals(shermanExpected.getStudentNumber(), ((Student) shermanMap.get("student")).getStudentNumber());
assertEquals(yogiEnrolmentExpected.getSemester(), yogiMap.get("semester"));
assertEquals(yogiEnrolmentExpected.getYear(), yogiMap.get("year"));
assertEquals(courseExpected, yogiMap.get("course"));
assertEquals(shermanEnrolmentExpected.getSemester(), shermanMap.get("semester"));
assertEquals(shermanEnrolmentExpected.getYear(), shermanMap.get("year"));
assertEquals(courseExpected, shermanMap.get("course"));
};
runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
Aggregations