use of jakarta.persistence.criteria.Selection in project hibernate-orm by hibernate.
the class TreatListJoinTest method testTreatJoin.
@Test
public void testTreatJoin() {
EntityManager em = createEntityManager();
try {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Tuple> query = cb.createTupleQuery();
final Root<TestEntity> testEntity = query.from(TestEntity.class);
final List<Selection<?>> selections = new LinkedList();
selections.add(testEntity.get("id"));
final ListJoin<TestEntity, AbstractEntity> entities = testEntity.joinList("entities", JoinType.LEFT);
entities.on(cb.equal(entities.get("entityType"), EntityA.class.getName()));
final ListJoin<TestEntity, EntityA> joinEntityA = cb.treat(entities, EntityA.class);
selections.add(joinEntityA.get("id"));
selections.add(joinEntityA.get("valueA"));
final ListJoin<TestEntity, AbstractEntity> entitiesB = testEntity.joinList("entities", JoinType.LEFT);
entitiesB.on(cb.equal(entitiesB.get("entityType"), EntityB.class.getName()));
final ListJoin<TestEntity, EntityB> joinEntityB = cb.treat(entitiesB, EntityB.class);
selections.add(joinEntityB.get("id"));
selections.add(joinEntityB.get("valueB"));
query.multiselect(selections);
final List<Tuple> resultList = em.createQuery(query).getResultList();
assertThat(resultList.size(), is(10));
} finally {
em.close();
}
}
use of jakarta.persistence.criteria.Selection 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 jakarta.persistence.criteria.Selection in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testMultiSelectNewMapUsingAliasesWithFetchJoinList.
@Test
public void testMultiSelectNewMapUsingAliasesWithFetchJoinList(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(Map.class);
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 hqlSelectNewMapExecutor = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select new map(s as s, pc as pc) from Student s left join s.preferredCourse pc left join fetch s.enrolments order by s.studentNumber");
}
};
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(), yogiMap.get("pc"));
Map shermanMap = (Map) resultList.get(1);
assertEquals(shermanExpected, shermanMap.get("s"));
assertNull(shermanMap.get("pc"));
if (areDynamicNonLazyAssociationsChecked()) {
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(hqlSelectNewMapExecutor, criteriaExecutor, checker, false, scope);
}
use of jakarta.persistence.criteria.Selection 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 jakarta.persistence.criteria.Selection in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuiteBase method testCriteriaBuilderArrayValidation.
// 366206 - CriteriaBuilder.array(Selection)
public void testCriteriaBuilderArrayValidation() {
EntityManager em = createEntityManager();
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Object[]> cquery = qb.createQuery(Object[].class);
Root<Employee> emp = wrapper.from(cquery, Employee.class);
Selection[] s = { wrapper.get(emp, Employee_id), wrapper.get(emp, Employee_lastName), wrapper.get(emp, Employee_firstName) };
Selection<Object[]> item = qb.array(s);
cquery.select(item);
TypedQuery<Object[]> query = em.createQuery(cquery);
// verify they work and can be used:
List<Object[]> list = query.getResultList();
list.get(0);
try {
// verify tuple throws an exception when passed an array
Object unexpectedResult = qb.tuple(item);
fail("IllegalArgumentException expected using an invalid value to CriteriaBuilder.tuple(). Result returned:" + unexpectedResult);
} catch (Exception iae) {
assertEquals(iae.getClass(), IllegalArgumentException.class);
}
try {
// verify array throws an exception when passed an array
Object unexpectedResult = qb.array(item);
fail("IllegalArgumentException expected using an invalid value to CriteriaBuilder.array(). Result returned:" + unexpectedResult);
} catch (Exception iae) {
assertEquals(iae.getClass(), IllegalArgumentException.class);
}
closeEntityManager(em);
}
Aggregations