Commit 2d7b9aad89368f09e2050a775ce7b0de7921f0d0
1 parent
7af45fb1
Exists in
master
Ajustes do PullRequest anterior:
- Uso de metodos protecteds - Renomeação dos métodos para findByJPQL e findByCriteriaQuery - Ajustes no Javadoc Correção do método findByJPQL que não versão anterior não funcionava corretamente.
Showing
1 changed file
with
28 additions
and
29 deletions
Show diff stats
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/template/JPACrud.java
... | ... | @@ -175,46 +175,45 @@ public class JPACrud<T, I> implements Crud<T, I> { |
175 | 175 | List<T> lista = query.getResultList(); |
176 | 176 | return lista; |
177 | 177 | } |
178 | - | |
178 | + | |
179 | 179 | /** |
180 | - * Perform a paged query by JPQL | |
181 | - * @param jpql | |
182 | - * @return | |
180 | + * Search JPQL integrated into the context of paging | |
181 | + * @param jpql - query in syntax JPQL | |
182 | + * @return a list of entities | |
183 | 183 | */ |
184 | - public List<T> findJPQL(String jpql) { | |
184 | + protected List<T> findByJPQL(String jpql) { | |
185 | + pagination = getPagination(); | |
185 | 186 | TypedQuery<T> listQuery = getEntityManager().createQuery(jpql, getBeanClass()); |
186 | - Pagination pagination = getPagination(); | |
187 | 187 | if (pagination != null) { |
188 | - CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); | |
189 | - CriteriaQuery<Long> countQuery = builder.createQuery(Long.class); | |
190 | - countQuery.select(builder.count(countQuery.from(getBeanClass()))); | |
191 | - getEntityManager().createQuery(jpql, getBeanClass()); | |
192 | - pagination.setTotalResults((int) (getEntityManager().createQuery(countQuery).getSingleResult() + 0)); | |
188 | + int indexFrom = jpql.toUpperCase().indexOf("FROM"); | |
189 | + String countJPQL = "SELECT count(*) " + jpql.substring(indexFrom); | |
190 | + Query query= getEntityManager().createQuery(countJPQL); | |
191 | + Number cResults=(Number) query.getSingleResult(); | |
192 | + pagination.setTotalResults(cResults.intValue()); | |
193 | 193 | listQuery.setFirstResult(pagination.getFirstResult()); |
194 | 194 | listQuery.setMaxResults(pagination.getPageSize()); |
195 | 195 | } |
196 | 196 | return listQuery.getResultList(); |
197 | 197 | } |
198 | - | |
198 | + | |
199 | 199 | /** |
200 | - * Perform a paged query by CriteriaQuery | |
201 | - * @param jpql | |
202 | - * @return | |
200 | + * Search CriteriaQuery integrated into the context of paging | |
201 | + * @param criteriaQuery - structure CriteriaQuery | |
202 | + * @return a list of entities | |
203 | 203 | */ |
204 | - public List<T> findCriteria(CriteriaQuery<T> select) { | |
205 | - CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); | |
206 | - | |
207 | - CriteriaQuery<Long> countQuery = builder.createQuery(Long.class); | |
208 | - countQuery.select(builder.count(countQuery.from(getBeanClass()))); | |
209 | - getEntityManager().createQuery(countQuery); | |
210 | - | |
211 | - Pagination p = getPagination(); | |
212 | - p.setTotalResults((int) (getEntityManager().createQuery(countQuery).getSingleResult() + 0)); | |
213 | - | |
214 | - TypedQuery<T> listQuery = getEntityManager().createQuery(select); | |
215 | - listQuery.setFirstResult(p.getFirstResult()); | |
216 | - listQuery.setMaxResults(p.getPageSize()); | |
217 | - | |
204 | + public List<T> findByCriteriaQuery(CriteriaQuery<T> criteriaQuery, List<Predicate> predicates) { | |
205 | + pagination = getPagination(); | |
206 | + TypedQuery<T> listQuery = getEntityManager().createQuery(criteriaQuery); | |
207 | + if (pagination != null) { | |
208 | + CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); | |
209 | + CriteriaQuery<Long> countQuery = builder.createQuery(Long.class); | |
210 | + countQuery.select(builder.count(countQuery.from(getBeanClass()))); | |
211 | + countQuery.where(predicates.toArray(new Predicate[] {})); | |
212 | + getEntityManager().createQuery(countQuery); | |
213 | + pagination.setTotalResults((int) (getEntityManager().createQuery(countQuery).getSingleResult() + 0)); | |
214 | + listQuery.setFirstResult(pagination.getFirstResult()); | |
215 | + listQuery.setMaxResults(pagination.getPageSize()); | |
216 | + } | |
218 | 217 | return listQuery.getResultList(); |
219 | 218 | } |
220 | 219 | ... | ... |