use of org.hibernate.query.criteria.JpaRoot in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testAliasToEntityMapMultiAggregatedPropProjectionSingleResult.
@Test
public void testAliasToEntityMapMultiAggregatedPropProjectionSingleResult(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(builder.min(root.get("studentNumber")).alias("minStudentNumber"), builder.max(root.get("studentNumber")).alias("maxStudentNumber"));
return criteria;
// return s.createCriteria( Enrolment.class )
// .setProjection(
// Projections.projectionList()
// .add( Projections.min( "studentNumber" ).as( "minStudentNumber" ) )
// .add( Projections.max( "studentNumber" ).as( "maxStudentNumber" ) )
// )
// .setResultTransformer( Transformers.ALIAS_TO_ENTITY_MAP );
}
};
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").setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
}
};
ResultChecker checker = results -> {
assertTrue(results instanceof Map);
Map resultMap = (Map) results;
assertEquals(2, resultMap.size());
assertEquals(yogiExpected.getStudentNumber(), resultMap.get("minStudentNumber"));
assertEquals(shermanExpected.getStudentNumber(), resultMap.get("maxStudentNumber"));
};
runTest(hqlExecutor, criteriaExecutor, checker, true, scope);
}
use of org.hibernate.query.criteria.JpaRoot 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.JpaRoot 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);
}
use of org.hibernate.query.criteria.JpaRoot in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testJoinWithFetchJoinWithOwnerAndPropProjectedList.
@Test
public void testJoinWithFetchJoinWithOwnerAndPropProjectedList(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);
root.fetch("enrolments", JoinType.LEFT);
root.join("preferredCourse", JoinType.LEFT);
criteria.orderBy(builder.asc(root.get("studentNumber")));
criteria.multiselect(root, root.get("name"));
return criteria;
}
};
HqlExecutor hqlSelectNewMapExecutor = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select s, s.name from Student s left join fetch s.enrolments left join s.preferredCourse 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.getName(), yogiObjects[1]);
Object[] shermanObjects = (Object[]) resultList.get(1);
assertEquals(shermanExpected, shermanObjects[0]);
assertEquals(shermanExpected.getName(), shermanObjects[1]);
if (areDynamicNonLazyAssociationsChecked()) {
assertTrue(Hibernate.isInitialized(((Student) yogiObjects[0]).getEnrolments()));
assertEquals(yogiExpected.getEnrolments(), ((Student) yogiObjects[0]).getEnrolments());
assertTrue(Hibernate.isInitialized(((Student) shermanObjects[0]).getEnrolments()));
assertEquals(shermanExpected.getEnrolments(), (((Student) shermanObjects[0]).getEnrolments()));
}
};
runTest(hqlSelectNewMapExecutor, criteriaExecutor, checker, false, scope);
}
use of org.hibernate.query.criteria.JpaRoot in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testAliasToEntityMapMultiProjectionWithNullAliasList.
@Test
public void testAliasToEntityMapMultiProjectionWithNullAliasList(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"), root.get("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" ) )
// .add( Property.forName( "e.year" ) )
// .add( Property.forName( "e.course" ), "course" )
// )
// .addOrder( Order.asc( "e.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, e.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);
// TODO: following are initialized for hql and uninitialied for criteria; why?
// assertFalse( Hibernate.isInitialized( yogiMap.get( "student" ) ) );
// assertFalse( Hibernate.isInitialized( shermanMap.get( "student" ) ) );
assertTrue(yogiMap.get("student") instanceof Student);
assertEquals(yogiExpected.getStudentNumber(), ((Student) yogiMap.get("student")).getStudentNumber());
assertEquals(shermanExpected.getStudentNumber(), ((Student) shermanMap.get("student")).getStudentNumber());
assertNull(yogiMap.get("semester"));
assertNull(yogiMap.get("year"));
assertEquals(courseExpected, yogiMap.get("course"));
assertNull(shermanMap.get("semester"));
assertNull(shermanMap.get("year"));
assertEquals(courseExpected, shermanMap.get("course"));
};
runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
Aggregations