规范和分组

时间:2015-09-13 04:16:43

标签: spring jpa nhibernate criteria specifications

我有一个实现各种Specification of Specification类的类。在一个方法中,它被称为" Criteria Query"的groupBy。但是要在spring存储库中调用此规范,会生成下一个错误:

SEVERE: El Servlet.service() para el servlet [appServlet] en el contexto con ruta [/web_service] lanzó la excepción [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query] con causa raíz org.postgresql.util.PSQLException: ERROR: column "analista0_.id_analista" must appear in the GROUP BY clause or be used in an aggregate function

SQL查询是这样的:

select analista0_.id_analista as id56_, analista0_1_.apellido as apellido56_, analista0_1_.cedula as cedula56_, analista0_1_.id_ciudad as id10_56_, analista0_1_.correo as correo56_, analista0_1_.direccion as direccion56_, analista0_1_.estatus as estatus56_, analista0_1_.nombre as nombre56_, analista0_1_.telefono as telefono56_, analista0_1_.tipo_menu as tipo9_56_, analista0_.administrador as administ1_57_ from Analista analista0_ inner join persona analista0_1_ on analista0_.id_analista=analista0_1_.id left outer join requerimiento requerimie1_ on analista0_.id_analista=requerimie1_.id_analista where analista0_.administrador<>? and (requerimie1_.estatus in (? , ? , ? , ? , ?) or  not (exists (select requerimie2_.id_requerimiento from requerimiento requerimie2_ where analista0_.id_analista=requerimie2_.id_analista))) order by count(distinct requerimie1_.id_requerimiento) asc limit ?

显然,错误是针对group by clausule。 在这种情况下,如何实现GROUP BY子句?

此规范类:

&#13;
&#13;
public Specification<Analista> consultarCantRequerimientos(final List<String> estatus) {
  
		return new Specification<Analista>(){
          
			public Predicate toPredicate(Root<Analista> entity, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
				
				inicializar(entity, criteriaQuery, criteriaBuilder);
				
				
				Map<String, JoinType> entidades=new HashMap<String, JoinType>();
				entidades.put("requerimientos", JoinType.LEFT);
				Map<String, Join<?,?>> joins = crearJoins(entidades);
				
				
				Expression<Long> cantRequerimientos = criteriaBuilder.countDistinct(joins.get("requerimientos"));
				
				criteriaQuery.multiselect(new Selection[]{
						cantRequerimientos.alias("cantRequerimientos"),
						entity.get("id")
				});
				
				
				List<Predicate> restricciones = new ArrayList<Predicate>();
				restricciones.add(
						criteriaBuilder.notEqual(entity.get("administrador"), true)
				);
				if(estatus!=null && !estatus.isEmpty())
					restricciones.add(
							criteriaBuilder.or(
									joins.get("requerimientos").get("estatus").in(estatus),
									criteriaBuilder.isEmpty(entity.<List<?>>get("requerimientos"))
									)
							);
				
				
				List<Order> orders = new ArrayList<Order>();
				orders.add(criteriaBuilder.asc(cantRequerimientos));
				criteriaQuery.orderBy(orders);

				 List<Expression<?>> groupBy = new ArrayList<Expression<?>>();
				 groupBy.add(entity.get("id"));
				 
				 criteriaQuery.groupBy(groupBy);
	             return criteriaBuilder.and(restricciones.toArray(new Predicate[]{}));
			}
			
		};
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:-1)