Predicate order排序问题异常!
private Specification<Organization> buildSpecification(
final Map<String, Object> searchParams) {
Specification<Organization> spec = new Specification<Organization>() {
@Override
public Predicate toPredicate(Root<Organization> root, CriteriaQuery<?> cq,
CriteriaBuilder cb) {
Predicate allCondition = null;
String name = (String) searchParams.get("name");
String createTime = (String) searchParams
.get("createTime");
if (name != null && !"".equals(name)) {
Predicate condition = cb.like(
root.get("name").as(String.class), "%"
+ searchParams.get("name") + "%");
if (null == allCondition) {
//allCondition = cb.and(condition);
cq.where(cb.and(condition));
} else {
//allCondition = cb.and(allCondition, condition);
cq.where(cb.and(allCondition, condition));
}
}
//点列表排序
String sortName = (String) searchParams.get("sortName");
String sortOrder = (String) searchParams.get("sortOrder");
if(sortName!=null && sortOrder.equals("asc") && !"".equals(sortName)){
cq.orderBy((cb.asc(root.get(sortName).as(String.class))));
} else if(sortName!=null && sortOrder.equals("desc") && !"".equals(sortName)){
cq.orderBy(cb.desc(root.get(sortName).as(String.class)));
}
// return allCondition;
return cq.getRestriction();
}
};
return spec;
}
点击列表 传入列明 name 排序desc 出现异常
1:03:48.376 [http-8082-1] DEBUG o.h.e.jdbc.spi.SqlExceptionHelper - could not extract ResultSet [n/a]
org.postgresql.util.PSQLException: ERROR: column "organizati0_.name" must appear in the GROUP BY clause or be used in an aggregate function
位置:87
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2458) ~[postgresql-9.4.1211.jar:9.4.1211]
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2158) ~[postgresql-9.4.1211.jar:9.4.1211]
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:291) ~[postgresql-9.4.1211.jar:9.4.1211]
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:432) ~[postgresql-9.4.1211.jar:9.4.1211]
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:358) ~[postgresql-9.4.1211.jar:9.4.1211]
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:171) ~[postgresql-9.4.1211.jar:9.4.1211]