use of jakarta.persistence.criteria.Order in project hibernate-orm by hibernate.
the class SqmSelectStatement method orderBy.
@Override
public SqmSelectStatement<T> orderBy(Order... orders) {
final SqmOrderByClause sqmOrderByClause = new SqmOrderByClause(orders.length);
for (Order order : orders) {
sqmOrderByClause.addSortSpecification((SqmSortSpecification) order);
}
getQueryPart().setOrderByClause(sqmOrderByClause);
return this;
}
use of jakarta.persistence.criteria.Order in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testAliasToEntityMapNoProjectionNullAndNonNullAliasList.
@Test
public void testAliasToEntityMapNoProjectionNullAndNonNullAliasList(SessionFactoryScope scope) throws Exception {
CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {
@Override
protected JpaCriteriaQuery getCriteria(Session s) {
final CriteriaBuilder builder = s.getSessionFactory().getCriteriaBuilder();
final JpaCriteriaQuery<Student> criteria = (JpaCriteriaQuery<Student>) builder.createQuery(Student.class);
final Root<Student> studentRoot = criteria.from(Student.class);
studentRoot.alias("s");
studentRoot.join("addresses", JoinType.LEFT).alias("a");
studentRoot.join("preferredCourse", JoinType.INNER);
criteria.orderBy(builder.asc(studentRoot.get("studentNumber")));
return criteria;
// return s.createCriteria( Student.class, "s" )
// .createAlias( "s.addresses", "a", CriteriaSpecification.LEFT_JOIN )
// .setResultTransformer( CriteriaSpecification.ALIAS_TO_ENTITY_MAP )
// .createCriteria( "s.preferredCourse", CriteriaSpecification.INNER_JOIN )
// .addOrder( Order.asc( "s.studentNumber") );
}
};
HqlExecutor hqlExecutor = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("from Student s left join s.addresses a left join s.preferredCourse order by s.studentNumber").setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
Map yogiMap1 = (Map) resultList.get(0);
assertEquals(2, yogiMap1.size());
Map yogiMap2 = (Map) resultList.get(1);
assertEquals(2, yogiMap2.size());
assertEquals(yogiExpected, yogiMap1.get("s"));
Address yogiAddress1 = (Address) yogiMap1.get("a");
assertEquals(yogiExpected.getAddresses().get(yogiAddress1.getAddressType()), yogiMap1.get("a"));
assertEquals(yogiExpected, yogiMap2.get("s"));
Address yogiAddress2 = (Address) yogiMap2.get("a");
assertEquals(yogiExpected.getAddresses().get(yogiAddress2.getAddressType()), yogiMap2.get("a"));
assertSame(yogiMap1.get("s"), yogiMap2.get("s"));
assertNotEquals(yogiAddress1.getAddressType(), yogiAddress2.getAddressType());
};
runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
use of jakarta.persistence.criteria.Order 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.Order 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 jakarta.persistence.criteria.Order in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testOneEntityProjectionList.
@Test
public void testOneEntityProjectionList(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(root.get("student"));
criteria.orderBy(builder.asc(root.get("studentNumber")));
return criteria;
// return s.createCriteria( Enrolment.class, "e" )
// .setProjection( Projections.property( "e.student" ) )
// .addOrder( Order.asc( "e.studentNumber") );
}
};
HqlExecutor hqlExecutor = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select e.student from Enrolment e order by e.studentNumber");
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
// TODO: following is initialized for hql and uninitialied for criteria; why?
// assertFalse( Hibernate.isInitialized( resultList.get( 0 ) ) );
// assertFalse( Hibernate.isInitialized( resultList.get( 1 ) ) );
assertEquals(yogiExpected.getStudentNumber(), ((Student) resultList.get(0)).getStudentNumber());
assertEquals(shermanExpected.getStudentNumber(), ((Student) resultList.get(1)).getStudentNumber());
};
runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
Aggregations