Commit 7af45fb1bea0aa51f8baf3b8f1dbc0f45f1eff59
1 parent
1537c49d
Exists in
master
inclusao dos metodos findJPQL e findCriteria genérico integrado ao
mecanismo de paginação do Demoiselle 2
Showing
1 changed file
with
44 additions
and
0 deletions
Show diff stats
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/template/JPACrud.java
| @@ -48,6 +48,7 @@ import javax.persistence.EntityManager; | @@ -48,6 +48,7 @@ import javax.persistence.EntityManager; | ||
| 48 | import javax.persistence.Enumerated; | 48 | import javax.persistence.Enumerated; |
| 49 | import javax.persistence.Query; | 49 | import javax.persistence.Query; |
| 50 | import javax.persistence.TransactionRequiredException; | 50 | import javax.persistence.TransactionRequiredException; |
| 51 | +import javax.persistence.TypedQuery; | ||
| 51 | import javax.persistence.criteria.CriteriaBuilder; | 52 | import javax.persistence.criteria.CriteriaBuilder; |
| 52 | import javax.persistence.criteria.CriteriaQuery; | 53 | import javax.persistence.criteria.CriteriaQuery; |
| 53 | import javax.persistence.criteria.Predicate; | 54 | import javax.persistence.criteria.Predicate; |
| @@ -174,6 +175,49 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -174,6 +175,49 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
| 174 | List<T> lista = query.getResultList(); | 175 | List<T> lista = query.getResultList(); |
| 175 | return lista; | 176 | return lista; |
| 176 | } | 177 | } |
| 178 | + | ||
| 179 | + /** | ||
| 180 | + * Perform a paged query by JPQL | ||
| 181 | + * @param jpql | ||
| 182 | + * @return | ||
| 183 | + */ | ||
| 184 | + public List<T> findJPQL(String jpql) { | ||
| 185 | + TypedQuery<T> listQuery = getEntityManager().createQuery(jpql, getBeanClass()); | ||
| 186 | + Pagination pagination = getPagination(); | ||
| 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)); | ||
| 193 | + listQuery.setFirstResult(pagination.getFirstResult()); | ||
| 194 | + listQuery.setMaxResults(pagination.getPageSize()); | ||
| 195 | + } | ||
| 196 | + return listQuery.getResultList(); | ||
| 197 | + } | ||
| 198 | + | ||
| 199 | + /** | ||
| 200 | + * Perform a paged query by CriteriaQuery | ||
| 201 | + * @param jpql | ||
| 202 | + * @return | ||
| 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 | + | ||
| 218 | + return listQuery.getResultList(); | ||
| 219 | + } | ||
| 220 | + | ||
| 177 | 221 | ||
| 178 | /** | 222 | /** |
| 179 | * Retrieves the number of persisted objects for the current class type. | 223 | * Retrieves the number of persisted objects for the current class type. |