use of org.hibernate.testing.orm.junit.SessionFactoryScope in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testEntityWithLazyAsList.
@Test
public void testEntityWithLazyAsList(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<Student> criteria = (JpaCriteriaQuery<Student>) builder.createQuery(Student.class);
JpaRoot<Student> root = criteria.from(Student.class);
criteria.orderBy(builder.asc(root.get("studentNumber")));
return criteria;
// return s.createCriteria( Student.class )
// .addOrder( Order.asc( "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());
assertNotNull(((Student) resultList.get(0)).getPreferredCourse());
assertNotNull(((Student) resultList.get(1)).getEnrolments());
assertNull(((Student) resultList.get(1)).getPreferredCourse());
assertFalse(Hibernate.isInitialized(((Student) resultList.get(0)).getEnrolments()));
assertFalse(Hibernate.isInitialized(((Student) resultList.get(0)).getPreferredCourse()));
assertFalse(Hibernate.isInitialized(((Student) resultList.get(1)).getEnrolments()));
assertNull(((Student) resultList.get(1)).getPreferredCourse());
};
runTest(null, criteriaExecutor, checker, false, scope);
}
use of org.hibernate.testing.orm.junit.SessionFactoryScope in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testMultiSelectAliasToEntityMapUsingAliasesWithFetchJoinList.
@Test
public void testMultiSelectAliasToEntityMapUsingAliasesWithFetchJoinList(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);
final Selection<Student> studentAlias = root.alias("s");
final Selection<Object> pcAlias = root.join("preferredCourse", JoinType.LEFT).alias("pc");
root.fetch("enrolments", JoinType.LEFT);
criteria.multiselect(studentAlias, pcAlias);
criteria.orderBy(builder.asc(root.get("studentNumber")));
return criteria;
// return s.createCriteria( Student.class, "s" )
// .createAlias( "s.preferredCourse", "pc", Criteria.LEFT_JOIN )
// .setFetchMode( "enrolments", FetchMode.JOIN )
// .addOrder( Order.asc( "s.studentNumber" ))
// .setResultTransformer( Transformers.ALIAS_TO_ENTITY_MAP );
}
};
HqlExecutor hqlAliasToEntityMapExecutor = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select s as s, pc as pc from Student s left join s.preferredCourse pc left join fetch s.enrolments order by s.studentNumber").setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
Map yogiMap = (Map) resultList.get(0);
assertEquals(yogiExpected, yogiMap.get("s"));
assertEquals(yogiExpected.getPreferredCourse().getCourseCode(), ((Course) yogiMap.get("pc")).getCourseCode());
Map shermanMap = (Map) resultList.get(1);
assertEquals(shermanExpected, shermanMap.get("s"));
assertNull(shermanMap.get("pc"));
if (areDynamicNonLazyAssociationsChecked()) {
assertEquals(yogiExpected.getPreferredCourse(), yogiMap.get("pc"));
assertTrue(Hibernate.isInitialized(((Student) yogiMap.get("s")).getEnrolments()));
assertEquals(yogiExpected.getEnrolments(), ((Student) yogiMap.get("s")).getEnrolments());
assertTrue(Hibernate.isInitialized(((Student) shermanMap.get("s")).getEnrolments()));
assertEquals(shermanExpected.getEnrolments(), (((Student) shermanMap.get("s")).getEnrolments()));
}
};
runTest(hqlAliasToEntityMapExecutor, criteriaExecutor, checker, false, scope);
}
use of org.hibernate.testing.orm.junit.SessionFactoryScope in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testSelectNewEntityConstructorList.
@Test
public void testSelectNewEntityConstructorList(SessionFactoryScope scope) throws Exception {
CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {
@Override
protected ResultTransformer getResultTransformer() {
return new AliasToBeanConstructorResultTransformer(getConstructor());
}
@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("name"));
criteria.orderBy(builder.asc(root.get("studentNumber")));
return criteria;
// return s.createCriteria( Student.class, "s" )
// .setProjection(
// Projections.projectionList()
// .add( Property.forName( "s.studentNumber" ).as( "studentNumber" ) )
// .add( Property.forName( "s.name" ).as( "name" ) )
// )
// .addOrder( Order.asc( "s.studentNumber" ) )
// .setResultTransformer( new AliasToBeanConstructorResultTransformer( getConstructor() ) );
}
private Constructor getConstructor() {
Type studentNametype = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor(Student.class.getName()).getPropertyType("name");
return ReflectHelper.getConstructor(Student.class, new Type[] { new BasicTypeImpl<>(LongJavaType.INSTANCE, BigIntJdbcType.INSTANCE), studentNametype });
}
};
HqlExecutor hqlExecutor = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select new Student(s.studentNumber, s.name) from Student s order by s.studentNumber");
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
Student yogi = (Student) resultList.get(0);
assertEquals(yogiExpected.getStudentNumber(), yogi.getStudentNumber());
assertEquals(yogiExpected.getName(), yogi.getName());
Student sherman = (Student) resultList.get(1);
assertEquals(shermanExpected.getStudentNumber(), sherman.getStudentNumber());
assertEquals(shermanExpected.getName(), sherman.getName());
};
runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
use of org.hibernate.testing.orm.junit.SessionFactoryScope in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testJoinWithFetchJoinListCriteria.
@Test
public void testJoinWithFetchJoinListCriteria(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<Student> criteria = (JpaCriteriaQuery<Student>) builder.createQuery(Student.class);
JpaRoot<Student> root = criteria.from(Student.class);
root.join("preferredCourse", JoinType.LEFT);
root.fetch("enrolments", JoinType.LEFT);
criteria.orderBy(builder.asc(root.get("studentNumber")));
return criteria;
// return s.createCriteria( Student.class, "s" )
// .createAlias( "s.preferredCourse", "pc", Criteria.LEFT_JOIN )
// .setFetchMode( "enrolments", FetchMode.JOIN )
// .addOrder( Order.asc( "s.studentNumber") );
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
assertEquals(yogiExpected, resultList.get(0));
// The following fails for criteria due to HHH-3524
// assertEquals( yogiExpected.getPreferredCourse(), ( ( Student ) resultList.get( 0 ) ).getPreferredCourse() );
assertEquals(yogiExpected.getPreferredCourse().getCourseCode(), ((Student) resultList.get(0)).getPreferredCourse().getCourseCode());
assertEquals(shermanExpected, resultList.get(1));
assertNull(((Student) resultList.get(1)).getPreferredCourse());
if (areDynamicNonLazyAssociationsChecked()) {
assertTrue(Hibernate.isInitialized(((Student) resultList.get(0)).getEnrolments()));
assertEquals(yogiExpected.getEnrolments(), ((Student) resultList.get(0)).getEnrolments());
assertTrue(Hibernate.isInitialized(((Student) resultList.get(1)).getEnrolments()));
assertEquals(shermanExpected.getEnrolments(), (((Student) resultList.get(1)).getEnrolments()));
}
};
runTest(null, criteriaExecutor, checker, false, scope);
}
use of org.hibernate.testing.orm.junit.SessionFactoryScope in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testSingleAggregatedPropProjectionSingleResult.
@Test
public void testSingleAggregatedPropProjectionSingleResult(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();
Root<Enrolment> root = criteria.from(Enrolment.class);
criteria.select(builder.min(root.get("studentNumber")));
return criteria;
// return s.createCriteria( Enrolment.class )
// .setProjection( Projections.min( "studentNumber" ) );
}
};
HqlExecutor hqlExecutor = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select min( e.studentNumber ) from Enrolment e");
}
};
ResultChecker checker = results -> {
assertTrue(results instanceof Long);
assertEquals(yogiExpected.getStudentNumber(), results);
};
runTest(hqlExecutor, criteriaExecutor, checker, true, scope);
}
Aggregations