11package org .baeldung .persistence .service ;
22
3- import static org .junit .Assert .assertEquals ;
4- import static org .junit .Assert .assertNull ;
5- import static org .junit .Assert .assertTrue ;
6-
3+ import static org .junit .Assert .*;
74import java .util .List ;
8-
95import javax .persistence .EntityManager ;
106import javax .persistence .EntityManagerFactory ;
117import javax .persistence .EntityTransaction ;
1511import javax .persistence .criteria .CriteriaBuilder ;
1612import javax .persistence .criteria .CriteriaQuery ;
1713import javax .persistence .criteria .Root ;
18-
19- import org .baeldung .persistence .model .Bar ;
20- import org .baeldung .persistence .model .Foo ;
21- import org .junit .After ;
14+ import com .google .common .collect .Lists ;
2215import org .junit .BeforeClass ;
2316import org .junit .Test ;
17+ import com .cc .jpa .example .Foo ;
18+ import com .cc .jpa .example .Bar ;
2419
2520public class FooServiceSortingTests {
26- private EntityManager entityManager ;
21+ private static EntityManager entityManager ;
22+ private static EntityManagerFactory emf ;
23+ private static EntityTransaction entityTransaction ;
24+ private static CriteriaBuilder criteriaBuilder ;
2725
2826 @ BeforeClass
2927 public static void before () {
30- //
31- }
3228
33- @ After
34- public final void after () {
35- //
29+ emf = Persistence .createEntityManagerFactory ("punit" );
30+ entityManager = emf .createEntityManager ();
31+ entityTransaction = entityManager .getTransaction ();
32+ entityTransaction .begin ();
33+ criteriaBuilder = entityManager .getCriteriaBuilder ();
3634 }
3735
3836 @ Test
3937 public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult () {
4038
41- final EntityManagerFactory emf = Persistence .createEntityManagerFactory ("punit" );
42- final EntityManager entityManager = emf .createEntityManager ();
43- final EntityTransaction entityTransaction = entityManager .getTransaction ();
44- entityTransaction .begin ();
4539 final String jql = "Select f from Foo as f order by f.id" ;
4640 final Query sortQuery = entityManager .createQuery (jql );
4741 final List <Foo > fooList = sortQuery .getResultList ();
48- assertEquals (1 , fooList .get (0 ).getId ());
49- assertEquals (100 , fooList .get (fooList .toArray ().length - 1 ).getId ());
5042 for (final Foo foo : fooList ) {
5143 System .out .println ("Name:" + foo .getName () + "-------Id:" + foo .getId ());
5244 }
45+
5346 }
5447
5548 @ Test
5649 public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult () {
5750
58- final EntityManagerFactory emf = Persistence .createEntityManagerFactory ("punit" );
59- final EntityManager entityManager = emf .createEntityManager ();
60- final EntityTransaction entityTransaction = entityManager .getTransaction ();
61- entityTransaction .begin ();
6251 final String jql = "Select f from Foo as f order by f.id desc" ;
6352 final Query sortQuery = entityManager .createQuery (jql );
6453 final List <Foo > fooList = sortQuery .getResultList ();
65- assertEquals (100 , fooList .get (0 ).getId ());
66- assertEquals (1 , fooList .get (fooList .toArray ().length - 1 ).getId ());
6754 for (final Foo foo : fooList ) {
6855 System .out .println ("Name:" + foo .getName () + "-------Id:" + foo .getId ());
6956 }
57+
7058 }
7159
7260 @ Test
7361 public final void whenSortingByTwoAttributes_thenPrintSortedResult () {
74- final EntityManagerFactory emf = Persistence .createEntityManagerFactory ("punit" );
75- final EntityManager entityManager = emf .createEntityManager ();
76- final EntityTransaction entityTransaction = entityManager .getTransaction ();
77- entityTransaction .begin ();
62+
7863 final String jql = "Select f from Foo as f order by f.name asc, f.id desc" ;
7964 final Query sortQuery = entityManager .createQuery (jql );
8065 final List <Foo > fooList = sortQuery .getResultList ();
8166 for (final Foo foo : fooList ) {
8267 System .out .println ("Name:" + foo .getName () + "-------Id:" + foo .getId ());
8368 }
69+
70+ }
71+
72+ @ Test
73+ public final void whenSortingFooByBar_thenBarsSorted () {
74+
75+ final String jql = "Select f from Foo as f order by f.name, f.bar.id" ;
76+ final Query barJoinQuery = entityManager .createQuery (jql );
77+ final List <Foo > fooList = barJoinQuery .getResultList ();
78+ for (final Foo foo : fooList ) {
79+ System .out .println ("Name:" + foo .getName () + "-------BarId:" + foo .getBar ().getId ());
80+ }
8481 }
8582
8683 @ Test
8784 public final void whenSortinfBar_thenPrintBarsSortedWithFoos () {
88- final EntityManagerFactory emf = Persistence .createEntityManagerFactory ("punit" );
89- final EntityManager entityManager = emf .createEntityManager ();
90- final EntityTransaction entityTransaction = entityManager .getTransaction ();
91- entityTransaction .begin ();
92- final String jql = "Select b from Bar as b order by b.id" ;
9385
86+ final String jql = "Select b from Bar as b order by b.id" ;
9487 final Query barQuery = entityManager .createQuery (jql );
9588 final List <Bar > barList = barQuery .getResultList ();
9689 for (final Bar bar : barList ) {
@@ -99,15 +92,12 @@ public final void whenSortinfBar_thenPrintBarsSortedWithFoos() {
9992 System .out .println ("FooName:" + foo .getName ());
10093 }
10194 }
95+
10296 }
10397
10498 @ Test
10599 public final void whenSortingByStringNullLast_thenLastNull () {
106100
107- final EntityManagerFactory emf = Persistence .createEntityManagerFactory ("punit" );
108- final EntityManager entityManager = emf .createEntityManager ();
109- final EntityTransaction entityTransaction = entityManager .getTransaction ();
110- entityTransaction .begin ();
111101 final String jql = "Select f from Foo as f order by f.name desc NULLS LAST" ;
112102 final Query sortQuery = entityManager .createQuery (jql );
113103 final List <Foo > fooList = sortQuery .getResultList ();
@@ -117,43 +107,34 @@ public final void whenSortingByStringNullLast_thenLastNull() {
117107 }
118108 }
119109
120- // @Test
121- // public final void whenSortingByStringNullFirst_thenFirstNull() {
122- // final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
123- // final EntityManager entityManager = emf.createEntityManager();
124- // final EntityTransaction entityTransaction = entityManager.getTransaction();
125- // entityTransaction.begin();
126- // final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST";
127- // final Query sortQuery = entityManager.createQuery(jql);
128- // final List<Foo> fooList = sortQuery.getResultList();
129- // assertNull(fooList.get(0).getName());
130- // for (final Foo foo : fooList) {
131- // System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getTest_Null());
132- // }
133- // }
134-
135110 @ Test
136- public final void whenSortingByIntNull_thenException () {
137- final EntityManagerFactory emf = Persistence .createEntityManagerFactory ("punit" );
138- final EntityManager entityManager = emf .createEntityManager ();
139- final EntityTransaction entityTransaction = entityManager .getTransaction ();
140- entityTransaction .begin ();
141- final String jql = "Select f from Foo as f order by f.test_Null desc NULLS FIRST" ;
111+ public final void whenSortingByStringNullFirst_thenFirstNull () {
112+
113+ final Foo nullNameFoo = new Foo ();
114+ nullNameFoo .setName (null );
115+
116+ final Bar bar = new Bar ();
117+ final List <Foo > fooList1 = Lists .newArrayList ();
118+ bar .setName ("Bar_Me" );
119+ nullNameFoo .setBar (bar );
120+ fooList1 .add (nullNameFoo );
121+ bar .setFooList (fooList1 );
122+ entityManager .persist (bar );
123+ entityManager .persist (nullNameFoo );
124+ entityTransaction .commit ();
125+ final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST" ;
142126 final Query sortQuery = entityManager .createQuery (jql );
143- boolean thrown = false ;
144- try {
145- final List <Foo > fooList = sortQuery .getResultList ();
146- } catch (final javax .persistence .PersistenceException e ) {
147- thrown = true ;
127+ final List <Foo > fooList = sortQuery .getResultList ();
128+ assertNull (fooList .get (0 ).getName ());
129+ for (final Foo foo : fooList ) {
130+ System .out .println ("Name:" + foo .getName ());
148131 }
149- assertTrue (thrown );
150132 }
151133
152134 @ Test
153135 public final void whenSortingFooWithCriteria_thenPrintSortedFoos () {
154- final EntityManagerFactory emf = Persistence .createEntityManagerFactory ("punit" );
155- final EntityManager entityManager = emf .createEntityManager ();
156- final CriteriaBuilder criteriaBuilder = entityManager .getCriteriaBuilder ();
136+
137+ criteriaBuilder = entityManager .getCriteriaBuilder ();
157138 final CriteriaQuery <Foo > criteriaQuery = criteriaBuilder .createQuery (Foo .class );
158139 final Root <Foo > from = criteriaQuery .from (Foo .class );
159140 final CriteriaQuery <Foo > select = criteriaQuery .select (from );
@@ -168,9 +149,8 @@ public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
168149
169150 @ Test
170151 public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos () {
171- final EntityManagerFactory emf = Persistence .createEntityManagerFactory ("punit" );
172- final EntityManager entityManager = emf .createEntityManager ();
173- final CriteriaBuilder criteriaBuilder = entityManager .getCriteriaBuilder ();
152+
153+ criteriaBuilder = entityManager .getCriteriaBuilder ();
174154 final CriteriaQuery <Foo > criteriaQuery = criteriaBuilder .createQuery (Foo .class );
175155 final Root <Foo > from = criteriaQuery .from (Foo .class );
176156 final CriteriaQuery <Foo > select = criteriaQuery .select (from );
0 commit comments