|
17 | 17 | import org.hibernate.SessionFactory; |
18 | 18 | import org.hibernate.criterion.Order; |
19 | 19 | import org.hibernate.criterion.Projections; |
20 | | -import org.hibernate.criterion.Restrictions; |
21 | 20 | import org.junit.After; |
22 | 21 | import org.junit.Before; |
23 | 22 | import org.junit.Test; |
@@ -149,49 +148,32 @@ public final void givenUsingTheScrollableApi_whenRetrievingPaginatedData_thenCor |
149 | 148 |
|
150 | 149 | @SuppressWarnings("unchecked") |
151 | 150 | @Test |
152 | | - public final void givenUsingTheScrollableApi_whenRetrievingPaginatedData_thenCorrect2() { |
153 | | - final int minId = 0; |
154 | | - final int maxId = 10; |
155 | | - |
156 | | - final Criteria criteriaCount = session.createCriteria(Foo.class, "FOO"); |
157 | | - criteriaCount.add(Restrictions.between("id", minId, maxId)); |
158 | | - criteriaCount.addOrder(Order.asc("id")); |
159 | | - criteriaCount.setProjection(Projections.rowCount()); |
160 | | - final Long count = (Long) criteriaCount.uniqueResult(); |
161 | | - |
162 | | - int pageNumber = 1; |
| 151 | + public final void givenUsingTheCriteriaApi_whenRetrievingFirstPage_thenCorrect() { |
163 | 152 | final int pageSize = 10; |
164 | | - final List<Foo> fooList = Lists.newArrayList(); |
165 | 153 |
|
166 | | - final Criteria criteria = session.createCriteria(Foo.class, "FOO"); |
167 | | - criteria.add(Restrictions.between("id", minId, maxId)); |
| 154 | + final Criteria criteria = session.createCriteria(Foo.class); |
168 | 155 | criteria.addOrder(Order.asc("id")); |
169 | | - int totalEntities = 0; |
170 | | - while (totalEntities < count.intValue()) { |
171 | | - criteria.setFirstResult((pageNumber - 1) * pageSize); |
172 | | - criteria.setMaxResults(pageSize); |
173 | | - fooList.addAll(criteria.list()); |
174 | | - totalEntities = fooList.size(); |
175 | | - pageNumber++; |
176 | | - } |
| 156 | + criteria.setFirstResult(0); |
| 157 | + criteria.setMaxResults(pageSize); |
| 158 | + final List<Foo> firstPage = criteria.list(); |
| 159 | + |
| 160 | + assertThat(firstPage, hasSize(pageSize)); |
177 | 161 | } |
178 | 162 |
|
| 163 | + @SuppressWarnings("unchecked") |
179 | 164 | @Test |
180 | | - public final void givenUsingTheScrollableApi_whenRetrievingPaginatedData_thenCorrect3() { |
181 | | - final int minId = 0; |
182 | | - final int maxId = 10; |
| 165 | + public final void givenUsingTheCriteriaApi_whenRetrievingPaginatedData_thenCorrect() { |
| 166 | + final Criteria criteriaCount = session.createCriteria(Foo.class); |
| 167 | + criteriaCount.addOrder(Order.asc("id")); |
| 168 | + criteriaCount.setProjection(Projections.rowCount()); |
| 169 | + final Long count = (Long) criteriaCount.uniqueResult(); |
| 170 | + |
183 | 171 | int pageNumber = 1; |
184 | 172 | final int pageSize = 10; |
185 | 173 | final List<Foo> fooList = Lists.newArrayList(); |
186 | 174 |
|
187 | | - final Criteria criteria = session.createCriteria(Foo.class, "FOO"); |
188 | | - final Criteria criteriaCount = session.createCriteria(Foo.class, "FOO"); |
189 | | - criteria.add(Restrictions.between("id", minId, maxId)); |
| 175 | + final Criteria criteria = session.createCriteria(Foo.class); |
190 | 176 | criteria.addOrder(Order.asc("id")); |
191 | | - criteriaCount.add(Restrictions.between("id", minId, maxId)); |
192 | | - criteriaCount.addOrder(Order.asc("id")); |
193 | | - criteriaCount.setProjection(Projections.rowCount()); |
194 | | - final Long count = (Long) criteriaCount.uniqueResult(); |
195 | 177 | int totalEntities = 0; |
196 | 178 | while (totalEntities < count.intValue()) { |
197 | 179 | criteria.setFirstResult((pageNumber - 1) * pageSize); |
|
0 commit comments