规范:
public Predicate toPredicate(Root<Hotel> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Predicate predicate = root.get("starRating").in(s);
if (unrated) {
predicate = cb.or(predicate, cb.isNull(root.get("starRating")));
}
return predicate;
}
哪里s
是 Set
.
SQL日志:
Hibernate: select hotel0_.hotel_id as hotel_id1_16_, hotel0_.star_rating as star_ra13_16_ where hotel0_.active=? and (hotel0_.star_rating in () or hotel0_.star_rating is null) order by lower(hotel0_.hotel_name) asc
错误:
ERROR 6828 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: syntax error at or near ")"
异常:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
我在这里缺少什么?
最佳答案
您的查询中的问题位于:hotel0_.star_ rating in ()
。
在此之前对 set 进行空检查:
if(s != null && !s.isEmpty()){
predicate = root.get("starRating").in(s);
}
关于java - spring data JPA CriteriaBuilder.isNull 返回语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53656264/