Skip to content

Commit 1c2e4f3

Browse files
committed
SortingTestsForArticle
1 parent a45c572 commit 1c2e4f3

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.baeldung.persistence.service;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.List;
6+
7+
import javax.persistence.EntityManager;
8+
import javax.persistence.EntityManagerFactory;
9+
import javax.persistence.EntityTransaction;
10+
import javax.persistence.Persistence;
11+
import javax.persistence.Query;
12+
13+
import org.hibernate.Session;
14+
import org.hibernate.SessionFactory;
15+
import org.junit.After;
16+
import org.junit.Before;
17+
import org.junit.BeforeClass;
18+
import org.junit.Test;
19+
20+
import com.cc.jpa.example.Foo;
21+
public class FooServiceSortingTests {
22+
private EntityManager entityManager;
23+
24+
@BeforeClass
25+
public static void before(){
26+
27+
28+
}
29+
30+
@After
31+
public final void after() {
32+
33+
}
34+
35+
@Test
36+
public final void whenSortingByOneAttributeDefault_thenSortedResult() {
37+
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){
43+
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId());
44+
}
45+
46+
}
47+
48+
@Test
49+
public final void whenSortingByOneAttribute_thenSortedResult() {
50+
51+
Query sortQuery = entityManager.createQuery
52+
("Select f from Foo as f order by f.id desc");
53+
List<Foo> fooList = sortQuery.getResultList();
54+
for(Foo foo:fooList){
55+
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId());
56+
}
57+
58+
}
59+
60+
@Test
61+
public final void whenSortingByTwoAttributes_thenSortedResult() {
62+
63+
Query sortQuery = entityManager.createQuery
64+
("Select f from Foo as f order by f.name asc, f.id desc");
65+
List<Foo> fooList = sortQuery.getResultList();
66+
for(Foo foo:fooList){
67+
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId());
68+
}
69+
70+
}
71+
72+
}

0 commit comments

Comments
 (0)