@@ -32,16 +32,15 @@ public final void after() {
3232
3333 }
3434
35- @ Test
36- public final void whenSortingByOneAttributeDefault_thenSortedResult () {
35+ @ Test
36+ public final void whenSortingByOneAttributeDefault_thenSortedResult () {
3737
38-
39- Query sortQuery = entityManager .createQuery
40- ("Select f from Foo as f order by f.id" );
41- List <Foo > fooList = sortQuery .getResultList ();
42- for (Foo foo :fooList ){
38+ Query sortQuery = entityManager .createQuery
39+ ("Select f from Foo as f order by f.id" );
40+ List <Foo > fooList = sortQuery .getResultList ();
41+ for (Foo foo :fooList ){
4342 System .out .println ("Name:" +foo .getName ()+"-------Id:" +foo .getId ());
44- }
43+ }
4544
4645 }
4746
@@ -68,5 +67,34 @@ public final void whenSortingByTwoAttributes_thenSortedResult() {
6867 }
6968
7069 }
70+
71+ @ Test
72+ public final void whenSortingFooWithCriteria_thenSortedFoos (){
73+
74+ Root <Foo > from = criteriaQuery .from (Foo .class );
75+ CriteriaQuery <Foo > select = criteriaQuery .select (from );
76+ criteriaQuery .orderBy (criteriaBuilder .asc (from .get ("name" )));
77+ TypedQuery <Foo > typedQuery = entityManager .createQuery (select );
78+ List <Foo >fooList = typedQuery .getResultList ();
79+ for (Foo foo :fooList ){
80+ System .out .println ("Name:" +foo .getName ()+"--------Id:" +foo .getId ());
81+ }
82+
83+ }
84+
85+ @ Test
86+ public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenSortedFoos (){
87+
88+ Root <Foo > from = criteriaQuery .from (Foo .class );
89+ CriteriaQuery <Foo > select = criteriaQuery .select (from );
90+ criteriaQuery .orderBy (criteriaBuilder .asc (from .get ("name" )), criteriaBuilder .desc (from .get ("id" )));
91+ TypedQuery <Foo > typedQuery = entityManager .createQuery (select );
92+ List <Foo >fooList = typedQuery .getResultList ();
93+ for (Foo foo :fooList ){
94+ System .out .println ("Name:" +foo .getName ()+"-------Id:" +foo .getId ());
95+ }
96+ }
97+
98+
7199
72100}
0 commit comments