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