Commit e33812769e3919f20871115d9d8bc216bc6002df
1 parent
2d7b9aad
Exists in
master
inclusao do método 'createCountQueryString' e remoção do parâmetro
'List<Predicate> predicates' do método findByCriteriaQuery
Showing
2 changed files
with
29 additions
and
7 deletions
Show diff stats
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/template/JPACrud.java
... | ... | @@ -39,6 +39,8 @@ package br.gov.frameworkdemoiselle.template; |
39 | 39 | import java.lang.reflect.Field; |
40 | 40 | import java.util.ArrayList; |
41 | 41 | import java.util.List; |
42 | +import java.util.regex.Matcher; | |
43 | +import java.util.regex.Pattern; | |
42 | 44 | |
43 | 45 | import javax.enterprise.inject.Instance; |
44 | 46 | import javax.inject.Inject; |
... | ... | @@ -185,10 +187,9 @@ public class JPACrud<T, I> implements Crud<T, I> { |
185 | 187 | pagination = getPagination(); |
186 | 188 | TypedQuery<T> listQuery = getEntityManager().createQuery(jpql, getBeanClass()); |
187 | 189 | if (pagination != null) { |
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(); | |
190 | + String countQueryString = createCountQueryString(jpql); | |
191 | + Query query = getEntityManager().createQuery(countQueryString); | |
192 | + Number cResults = (Number) query.getSingleResult(); | |
192 | 193 | pagination.setTotalResults(cResults.intValue()); |
193 | 194 | listQuery.setFirstResult(pagination.getFirstResult()); |
194 | 195 | listQuery.setMaxResults(pagination.getPageSize()); |
... | ... | @@ -201,14 +202,14 @@ public class JPACrud<T, I> implements Crud<T, I> { |
201 | 202 | * @param criteriaQuery - structure CriteriaQuery |
202 | 203 | * @return a list of entities |
203 | 204 | */ |
204 | - public List<T> findByCriteriaQuery(CriteriaQuery<T> criteriaQuery, List<Predicate> predicates) { | |
205 | + public List<T> findByCriteriaQuery(CriteriaQuery<T> criteriaQuery) { | |
205 | 206 | pagination = getPagination(); |
206 | 207 | TypedQuery<T> listQuery = getEntityManager().createQuery(criteriaQuery); |
207 | 208 | if (pagination != null) { |
208 | 209 | CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); |
209 | 210 | CriteriaQuery<Long> countQuery = builder.createQuery(Long.class); |
210 | 211 | countQuery.select(builder.count(countQuery.from(getBeanClass()))); |
211 | - countQuery.where(predicates.toArray(new Predicate[] {})); | |
212 | + countQuery.where(criteriaQuery.getRestriction()); | |
212 | 213 | getEntityManager().createQuery(countQuery); |
213 | 214 | pagination.setTotalResults((int) (getEntityManager().createQuery(countQuery).getSingleResult() + 0)); |
214 | 215 | listQuery.setFirstResult(pagination.getFirstResult()); |
... | ... | @@ -217,6 +218,26 @@ public class JPACrud<T, I> implements Crud<T, I> { |
217 | 218 | return listQuery.getResultList(); |
218 | 219 | } |
219 | 220 | |
221 | + /** | |
222 | + * Converts JPA query to JPA count query | |
223 | + * @param query | |
224 | + * @return | |
225 | + */ | |
226 | + private String createCountQueryString(String query) { | |
227 | + Matcher matcher = Pattern.compile("SELECT(.+)FROM").matcher(query); | |
228 | + if (matcher.find()){ | |
229 | + String group = matcher.group(1).trim(); | |
230 | + query = query.replaceFirst(group, "COUNT(" + group + ")"); | |
231 | + matcher = Pattern.compile("ORDER(.+)").matcher(query); | |
232 | + if (matcher.find()){ | |
233 | + group = matcher.group(1); | |
234 | + query = query.replaceFirst("ORDER"+group, ""); | |
235 | + } | |
236 | + return query; | |
237 | + }else{ | |
238 | + throw new DemoiselleException(bundle.get().getString("malformed-jpql")); | |
239 | + } | |
240 | + } | |
220 | 241 | |
221 | 242 | /** |
222 | 243 | * Retrieves the number of persisted objects for the current class type. | ... | ... |
impl/extension/jpa/src/main/resources/demoiselle-jpa-bundle.properties
... | ... | @@ -41,4 +41,5 @@ can-not-get-persistence-unit-from-persistence=N\u00E3o foi poss\u00EDvel obter a |
41 | 41 | more-than-one-persistence-unit-defined=Existe mais de uma unidade de persist\u00EAncia definida. Utilize @{0} no ponto de inje\u00E7\u00E3o ou defina o atributo "frameworkdemoiselle.persistence.unit.name" no arquivo demoiselle.properties. |
42 | 42 | persistence-unit-name-found=Unidade de persist\u00EAncia "{0}" encontrada. |
43 | 43 | entity-manager-closed=O gerenciador de entidades foi fechado. |
44 | -no-transaction-active=Nenhuma transa\u00E7\u00E3o est\u00E1 ativa, verifique a configura\u00E7\u00E3o "{0}" no arquivo "{1}" e defina a sua estrat\u00E9gia de transa\u00E7\u00E3o. | |
45 | 44 | \ No newline at end of file |
45 | +no-transaction-active=Nenhuma transa\u00E7\u00E3o est\u00E1 ativa, verifique a configura\u00E7\u00E3o "{0}" no arquivo "{1}" e defina a sua estrat\u00E9gia de transa\u00E7\u00E3o. | |
46 | +malformed-jpql=Consulta JPQL mal formada para pagina\u00E7\u00E3o de dados. | ... | ... |