diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java index b808a5e36b..a6dc8bd1b8 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java @@ -24,7 +24,6 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; - @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java index 03162b8447..154d529881 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java +++ b/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java @@ -4,6 +4,7 @@ import java.io.Serializable; import java.util.List; import javax.persistence.CascadeType; +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; @@ -14,43 +15,40 @@ import javax.persistence.OrderBy; @Entity public class Bar implements Serializable { - private static final long serialVersionUID = 1L; + @Id @GeneratedValue(strategy = GenerationType.AUTO) - private int id; + private long id; + + @Column(nullable = false) + private String name; + @OneToMany(mappedBy = "bar", fetch = FetchType.EAGER, cascade = CascadeType.ALL) @OrderBy("name ASC") List fooList; - private String name; - public Bar(){ + public Bar() { super(); } - public Bar(final String name){ + public Bar(final String name) { super(); + this.name = name; } - //API + // API - public List getFooList() { - return fooList; - } - - public void setFooList(final List fooList) { - this.fooList = fooList; - } - - public int getId() { + public long getId() { return id; } - public void setId(final int id) { + public void setId(final long id) { + this.id = id; } @@ -62,6 +60,14 @@ public class Bar implements Serializable { this.name = name; } + public List getFooList() { + return fooList; + } + + public void setFooList(final List fooList) { + this.fooList = fooList; + } + // @Override 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 1a15faf2c1..baba4037f1 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 @@ -29,7 +29,6 @@ public class FooServiceSortingTests { @BeforeClass public static void before() { - emf = Persistence.createEntityManagerFactory("punit"); entityManager = emf.createEntityManager(); entityTransaction = entityManager.getTransaction(); @@ -39,43 +38,36 @@ public class FooServiceSortingTests { @Test public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() { - final String jql = "Select f from Foo as f order by f.id"; 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 whenSortingByOneAttributeSetOrder_thenSortedPrintResult() { - 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(); for (final Foo foo : fooList) { System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId()); } - } @Test public final void whenSortingByTwoAttributes_thenPrintSortedResult() { - 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(); @@ -86,7 +78,6 @@ public class FooServiceSortingTests { @Test public final void whenSortinfBar_thenPrintBarsSortedWithFoos() { - final String jql = "Select b from Bar as b order by b.id"; final Query barQuery = entityManager.createQuery(jql); final List barList = barQuery.getResultList(); @@ -96,12 +87,10 @@ public class FooServiceSortingTests { System.out.println("FooName:" + foo.getName()); } } - } @Test public final void whenSortingByStringNullLast_thenLastNull() { - 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(); @@ -113,7 +102,6 @@ public class FooServiceSortingTests { @Test public final void whenSortingByStringNullFirst_thenFirstNull() { - final Foo nullNameFoo = new Foo(); nullNameFoo.setName(null); @@ -137,7 +125,6 @@ public class FooServiceSortingTests { @Test public final void whenSortingFooWithCriteria_thenPrintSortedFoos() { - criteriaBuilder = entityManager.getCriteriaBuilder(); final CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Foo.class); final Root from = criteriaQuery.from(Foo.class); @@ -148,12 +135,10 @@ public class FooServiceSortingTests { for (final Foo foo : fooList) { System.out.println("Name:" + foo.getName() + "--------Id:" + foo.getId()); } - } @Test public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() { - criteriaBuilder = entityManager.getCriteriaBuilder(); final CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Foo.class); final Root from = criteriaQuery.from(Foo.class);