|
7 | 7 | import javax.persistence.EntityManager; |
8 | 8 | import javax.persistence.EntityManagerFactory; |
9 | 9 | import javax.persistence.EntityTransaction; |
| 10 | +import javax.persistence.OrderBy; |
10 | 11 | import javax.persistence.Persistence; |
11 | 12 | import javax.persistence.Query; |
| 13 | +import javax.persistence.TypedQuery; |
| 14 | +import javax.persistence.criteria.CriteriaBuilder; |
| 15 | +import javax.persistence.criteria.CriteriaQuery; |
| 16 | +import javax.persistence.criteria.Order; |
| 17 | +import javax.persistence.criteria.Root; |
12 | 18 |
|
13 | 19 | import org.hibernate.Session; |
14 | 20 | import org.hibernate.SessionFactory; |
| 21 | +import org.hibernate.annotations.common.util.StringHelper; |
15 | 22 | import org.junit.After; |
16 | 23 | import org.junit.Before; |
17 | 24 | import org.junit.BeforeClass; |
18 | 25 | import org.junit.Test; |
19 | 26 |
|
20 | 27 | import com.cc.jpa.example.Foo; |
| 28 | +import com.cc.jpa.example.Bar; |
| 29 | + |
| 30 | + |
21 | 31 | public class FooServiceSortingTests { |
22 | | -private EntityManager entityManager; |
23 | | - |
| 32 | + private EntityManager entityManager; |
| 33 | + |
24 | 34 | @BeforeClass |
25 | 35 | public static void before(){ |
26 | | - |
27 | | - |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + @After |
| 40 | + public final void after() { |
| 41 | + |
28 | 42 | } |
29 | 43 |
|
30 | | - @After |
31 | | - public final void after() { |
32 | | - |
33 | | - } |
34 | | - |
35 | | - @Test |
36 | | - public final void whenSortingByOneAttributeDefault_thenSortedResult() { |
37 | | - |
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){ |
42 | | - System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); |
43 | | - } |
44 | | - |
| 44 | + @Test |
| 45 | + public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() { |
| 46 | + |
| 47 | + EntityManagerFactory emf = Persistence |
| 48 | + .createEntityManagerFactory("punit"); |
| 49 | + EntityManager entityManager = emf.createEntityManager(); |
| 50 | + EntityTransaction entityTransaction = entityManager |
| 51 | + .getTransaction(); |
| 52 | + entityTransaction.begin(); |
| 53 | + String jql = "Select f from Foo as f order by f.id"; |
| 54 | + Query sortQuery = entityManager.createQuery |
| 55 | + (jql); |
| 56 | + List<Foo> fooList = sortQuery.getResultList(); |
| 57 | + assertEquals(1,fooList.get(0).getId()); |
| 58 | + assertEquals(100,fooList.get(fooList.toArray().length-1).getId()); |
| 59 | + for(Foo foo:fooList){ |
| 60 | + System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); |
| 61 | + } |
| 62 | + |
45 | 63 | } |
46 | | - |
| 64 | + |
47 | 65 | @Test |
48 | | - public final void whenSortingByOneAttribute_thenSortedResult() { |
49 | | - |
| 66 | + public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() { |
| 67 | + |
| 68 | + EntityManagerFactory emf = Persistence |
| 69 | + .createEntityManagerFactory("punit"); |
| 70 | + EntityManager entityManager = emf.createEntityManager(); |
| 71 | + EntityTransaction entityTransaction = entityManager |
| 72 | + .getTransaction(); |
| 73 | + entityTransaction.begin(); |
| 74 | + String jql = "Select f from Foo as f order by f.id desc"; |
50 | 75 | Query sortQuery = entityManager.createQuery |
51 | | - ("Select f from Foo as f order by f.id desc"); |
| 76 | + (jql); |
52 | 77 | List<Foo> fooList = sortQuery.getResultList(); |
| 78 | + assertEquals(100,fooList.get(0).getId()); |
| 79 | + assertEquals(1,fooList.get(fooList.toArray().length-1).getId()); |
53 | 80 | for(Foo foo:fooList){ |
54 | 81 | System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); |
55 | 82 | } |
56 | | - |
| 83 | + |
57 | 84 | } |
58 | | - |
| 85 | + |
59 | 86 | @Test |
60 | | - public final void whenSortingByTwoAttributes_thenSortedResult() { |
61 | | - |
| 87 | + public final void whenSortingByTwoAttributes_thenPrintSortedResult() { |
| 88 | + |
| 89 | + EntityManagerFactory emf = Persistence |
| 90 | + .createEntityManagerFactory("punit"); |
| 91 | + EntityManager entityManager = emf.createEntityManager(); |
| 92 | + EntityTransaction entityTransaction = entityManager |
| 93 | + .getTransaction(); |
| 94 | + entityTransaction.begin(); |
| 95 | + String jql = "Select f from Foo as f order by f.name asc, f.id desc"; |
62 | 96 | Query sortQuery = entityManager.createQuery |
63 | | - ("Select f from Foo as f order by f.name asc, f.id desc"); |
| 97 | + (jql); |
64 | 98 | List<Foo> fooList = sortQuery.getResultList(); |
65 | 99 | for(Foo foo:fooList){ |
66 | 100 | System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); |
67 | 101 | } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public final void whenSortinfBar_thenPrintBarsSortedWithFoos(){ |
| 107 | + EntityManagerFactory emf = Persistence |
| 108 | + .createEntityManagerFactory("punit"); |
| 109 | + EntityManager entityManager = emf.createEntityManager(); |
| 110 | + EntityTransaction entityTransaction = entityManager |
| 111 | + .getTransaction(); |
| 112 | + entityTransaction.begin(); |
| 113 | + String jql = "Select b from Bar as b order by b.id"; |
68 | 114 |
|
| 115 | + Query barQuery = entityManager.createQuery(jql); |
| 116 | + List<Bar>barList = barQuery.getResultList(); |
| 117 | + for(Bar bar:barList){ |
| 118 | + System.out.println("Bar Id:"+bar.getId()); |
| 119 | + for(Foo foo:bar.getFooList()){ |
| 120 | + System.out.println("FooName:"+foo.getName()); |
| 121 | + } |
| 122 | + } |
| 123 | + |
69 | 124 | } |
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 | | - |
| 125 | + |
| 126 | + @Test |
| 127 | + public final void whenSortingByStringNullLast_thenLastNull() { |
| 128 | + |
| 129 | + EntityManagerFactory emf = Persistence |
| 130 | + .createEntityManagerFactory("punit"); |
| 131 | + EntityManager entityManager = emf.createEntityManager(); |
| 132 | + EntityTransaction entityTransaction = entityManager |
| 133 | + .getTransaction(); |
| 134 | + entityTransaction.begin(); |
| 135 | + String jql = "Select f from Foo as f order by f.name desc NULLS LAST"; |
| 136 | + Query sortQuery = entityManager.createQuery |
| 137 | + (jql); |
| 138 | + List<Foo> fooList = sortQuery.getResultList(); |
| 139 | + assertNull(fooList.get(fooList.toArray().length-1).getName()); |
| 140 | + for(Foo foo:fooList){ |
| 141 | + System.out.println("Name:"+foo.getName()); |
| 142 | + } |
| 143 | + } |
| 144 | + @Test |
| 145 | + public final void whenSortingByStringNullFirst_thenFirstNull() { |
99 | 146 |
|
| 147 | + EntityManagerFactory emf = Persistence |
| 148 | + .createEntityManagerFactory("punit"); |
| 149 | + EntityManager entityManager = emf.createEntityManager(); |
| 150 | + EntityTransaction entityTransaction = entityManager |
| 151 | + .getTransaction(); |
| 152 | + entityTransaction.begin(); |
| 153 | + String jql = "Select f from Foo as f order by f.name desc NULLS FIRST"; |
| 154 | + Query sortQuery = entityManager.createQuery |
| 155 | + (jql); |
| 156 | + List<Foo> fooList = sortQuery.getResultList(); |
| 157 | + assertNull(fooList.get(0).getName()); |
| 158 | + for(Foo foo:fooList){ |
| 159 | + System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getTest_Null()); |
| 160 | + } |
| 161 | + } |
| 162 | + @Test |
| 163 | + public final void whenSortingByIntNull_thenException() { |
| 164 | + |
| 165 | + EntityManagerFactory emf = Persistence |
| 166 | + .createEntityManagerFactory("punit"); |
| 167 | + EntityManager entityManager = emf.createEntityManager(); |
| 168 | + EntityTransaction entityTransaction = entityManager |
| 169 | + .getTransaction(); |
| 170 | + entityTransaction.begin(); |
| 171 | + String jql = "Select f from Foo as f order by f.test_Null desc NULLS FIRST"; |
| 172 | + Query sortQuery = entityManager.createQuery |
| 173 | + (jql); |
| 174 | + boolean thrown = false; |
| 175 | + try { |
| 176 | + List<Foo> fooList = sortQuery.getResultList(); |
| 177 | + } catch (javax.persistence.PersistenceException e) { |
| 178 | + thrown = true; |
| 179 | + } |
| 180 | + assertTrue(thrown); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public final void whenSortingFooWithCriteria_thenPrintSortedFoos(){ |
| 185 | + |
| 186 | + EntityManagerFactory emf = Persistence |
| 187 | + .createEntityManagerFactory("punit"); |
| 188 | + EntityManager entityManager = emf.createEntityManager(); |
| 189 | + CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); |
| 190 | + CriteriaQuery<Foo> criteriaQuery = criteriaBuilder |
| 191 | + .createQuery(Foo.class); |
| 192 | + Root<Foo> from = criteriaQuery.from(Foo.class); |
| 193 | + CriteriaQuery<Foo> select = criteriaQuery.select(from); |
| 194 | + criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name"))); |
| 195 | + TypedQuery<Foo> typedQuery = entityManager.createQuery(select); |
| 196 | + List<Foo>fooList = typedQuery.getResultList(); |
| 197 | + for(Foo foo:fooList){ |
| 198 | + System.out.println("Name:"+foo.getName()+"--------Id:"+foo.getId()); |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos(){ |
| 205 | + |
| 206 | + EntityManagerFactory emf = Persistence |
| 207 | + .createEntityManagerFactory("punit"); |
| 208 | + EntityManager entityManager = emf.createEntityManager(); |
| 209 | + CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); |
| 210 | + CriteriaQuery<Foo> criteriaQuery = criteriaBuilder |
| 211 | + .createQuery(Foo.class); |
| 212 | + Root<Foo> from = criteriaQuery.from(Foo.class); |
| 213 | + CriteriaQuery<Foo> select = criteriaQuery.select(from); |
| 214 | + criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")), criteriaBuilder.desc(from.get("id"))); |
| 215 | + TypedQuery<Foo> typedQuery = entityManager.createQuery(select); |
| 216 | + List<Foo>fooList = typedQuery.getResultList(); |
| 217 | + for(Foo foo:fooList){ |
| 218 | + System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); |
| 219 | + } |
| 220 | + } |
| 221 | + |
100 | 222 | } |
0 commit comments