diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java index 0cc8645af8..e1ecbd2015 100644 --- a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java +++ b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java @@ -1,11 +1,7 @@ package org.baeldung.persistence.service; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - +import static org.junit.Assert.*; import java.util.List; - import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; @@ -15,82 +11,79 @@ import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; - -import org.baeldung.persistence.model.Bar; -import org.baeldung.persistence.model.Foo; -import org.junit.After; +import com.google.common.collect.Lists; import org.junit.BeforeClass; import org.junit.Test; +import com.cc.jpa.example.Foo; +import com.cc.jpa.example.Bar; public class FooServiceSortingTests { - private EntityManager entityManager; + private static EntityManager entityManager; + private static EntityManagerFactory emf; + private static EntityTransaction entityTransaction; + private static CriteriaBuilder criteriaBuilder; @BeforeClass public static void before() { - // - } - @After - public final void after() { - // + emf = Persistence.createEntityManagerFactory("punit"); + entityManager = emf.createEntityManager(); + entityTransaction = entityManager.getTransaction(); + entityTransaction.begin(); + criteriaBuilder = entityManager.getCriteriaBuilder(); } @Test public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final EntityTransaction entityTransaction = entityManager.getTransaction(); - entityTransaction.begin(); final String jql = "Select f from Foo as f order by f.id"; final Query sortQuery = entityManager.createQuery(jql); final List fooList = sortQuery.getResultList(); - assertEquals(1, fooList.get(0).getId()); - assertEquals(100, fooList.get(fooList.toArray().length - 1).getId()); for (final Foo foo : fooList) { System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId()); } + } @Test public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final EntityTransaction entityTransaction = entityManager.getTransaction(); - entityTransaction.begin(); final String jql = "Select f from Foo as f order by f.id desc"; final Query sortQuery = entityManager.createQuery(jql); final List fooList = sortQuery.getResultList(); - assertEquals(100, fooList.get(0).getId()); - assertEquals(1, fooList.get(fooList.toArray().length - 1).getId()); for (final Foo foo : fooList) { System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId()); } + } @Test public final void whenSortingByTwoAttributes_thenPrintSortedResult() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final EntityTransaction entityTransaction = entityManager.getTransaction(); - entityTransaction.begin(); + final String jql = "Select f from Foo as f order by f.name asc, f.id desc"; final Query sortQuery = entityManager.createQuery(jql); final List fooList = sortQuery.getResultList(); for (final Foo foo : fooList) { System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId()); } + + } + + @Test + public final void whenSortingFooByBar_thenBarsSorted() { + + final String jql = "Select f from Foo as f order by f.name, f.bar.id"; + final Query barJoinQuery = entityManager.createQuery(jql); + final List fooList = barJoinQuery.getResultList(); + for (final Foo foo : fooList) { + System.out.println("Name:" + foo.getName() + "-------BarId:" + foo.getBar().getId()); + } } @Test public final void whenSortinfBar_thenPrintBarsSortedWithFoos() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final EntityTransaction entityTransaction = entityManager.getTransaction(); - entityTransaction.begin(); - final String jql = "Select b from Bar as b order by b.id"; + final String jql = "Select b from Bar as b order by b.id"; final Query barQuery = entityManager.createQuery(jql); final List barList = barQuery.getResultList(); for (final Bar bar : barList) { @@ -99,15 +92,12 @@ public class FooServiceSortingTests { System.out.println("FooName:" + foo.getName()); } } + } @Test public final void whenSortingByStringNullLast_thenLastNull() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final EntityTransaction entityTransaction = entityManager.getTransaction(); - entityTransaction.begin(); final String jql = "Select f from Foo as f order by f.name desc NULLS LAST"; final Query sortQuery = entityManager.createQuery(jql); final List fooList = sortQuery.getResultList(); @@ -117,43 +107,34 @@ public class FooServiceSortingTests { } } - // @Test - // public final void whenSortingByStringNullFirst_thenFirstNull() { - // final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - // final EntityManager entityManager = emf.createEntityManager(); - // final EntityTransaction entityTransaction = entityManager.getTransaction(); - // entityTransaction.begin(); - // final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST"; - // final Query sortQuery = entityManager.createQuery(jql); - // final List fooList = sortQuery.getResultList(); - // assertNull(fooList.get(0).getName()); - // for (final Foo foo : fooList) { - // System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getTest_Null()); - // } - // } - @Test - public final void whenSortingByIntNull_thenException() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final EntityTransaction entityTransaction = entityManager.getTransaction(); - entityTransaction.begin(); - final String jql = "Select f from Foo as f order by f.test_Null desc NULLS FIRST"; + public final void whenSortingByStringNullFirst_thenFirstNull() { + + final Foo nullNameFoo = new Foo(); + nullNameFoo.setName(null); + + final Bar bar = new Bar(); + final List fooList1 = Lists.newArrayList(); + bar.setName("Bar_Me"); + nullNameFoo.setBar(bar); + fooList1.add(nullNameFoo); + bar.setFooList(fooList1); + entityManager.persist(bar); + entityManager.persist(nullNameFoo); + entityTransaction.commit(); + final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST"; final Query sortQuery = entityManager.createQuery(jql); - boolean thrown = false; - try { - final List fooList = sortQuery.getResultList(); - } catch (final javax.persistence.PersistenceException e) { - thrown = true; + final List fooList = sortQuery.getResultList(); + assertNull(fooList.get(0).getName()); + for (final Foo foo : fooList) { + System.out.println("Name:" + foo.getName()); } - assertTrue(thrown); } @Test public final void whenSortingFooWithCriteria_thenPrintSortedFoos() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + + criteriaBuilder = entityManager.getCriteriaBuilder(); final CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Foo.class); final Root from = criteriaQuery.from(Foo.class); final CriteriaQuery select = criteriaQuery.select(from); @@ -168,9 +149,8 @@ public class FooServiceSortingTests { @Test public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit"); - final EntityManager entityManager = emf.createEntityManager(); - final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + + criteriaBuilder = entityManager.getCriteriaBuilder(); final CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Foo.class); final Root from = criteriaQuery.from(Foo.class); final CriteriaQuery select = criteriaQuery.select(from);