use of org.hibernate.query.criteria.JpaCriteriaQuery in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testAliasToBeanDtoWithNullAliasList.
@Test
public void testAliasToBeanDtoWithNullAliasList(SessionFactoryScope scope) throws Exception {
CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {
@Override
protected ResultTransformer getResultTransformer() {
return Transformers.aliasToBean(StudentDTO.class);
}
@Override
protected JpaCriteriaQuery getCriteria(Session s) {
CriteriaBuilder builder = s.getCriteriaBuilder();
JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery();
Root<Enrolment> root = criteria.from(Enrolment.class);
criteria.multiselect(root.get("student").get("name").alias("studentName"), root.get("student").get("studentNumber"), root.get("course").get("description").alias("courseDescription"));
criteria.orderBy(builder.asc(root.get("studentNumber")));
return criteria;
// return s.createCriteria( Enrolment.class, "e" )
// .createAlias( "e.student", "st" )
// .createAlias( "e.course", "co" )
// .setProjection(
// Projections.projectionList()
// .add( Property.forName( "st.name" ).as( "studentName" ) )
// .add( Property.forName( "st.studentNumber" ) )
// .add( Property.forName( "co.description" ).as( "courseDescription" ) )
// )
// .addOrder( Order.asc( "e.studentNumber" ) )
// .setResultTransformer( Transformers.aliasToBean( StudentDTO.class ) );
}
};
HqlExecutor hqlExecutor = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select st.name as studentName, co.description as courseDescription from Enrolment e join e.student st join e.course co order by e.studentNumber").setResultTransformer(Transformers.aliasToBean(StudentDTO.class));
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
StudentDTO dto = (StudentDTO) resultList.get(0);
assertEquals(courseExpected.getDescription(), dto.getDescription());
assertEquals(yogiExpected.getName(), dto.getName());
dto = (StudentDTO) resultList.get(1);
assertEquals(courseExpected.getDescription(), dto.getDescription());
assertEquals(shermanExpected.getName(), dto.getName());
};
runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
Aggregations