我启动了一个项目来学习Spring Data JPA。该查询将检索与数据库中的个人资料相关联的所有Contract
。
要求搜索条件必须是动态的:可以是空,空或值,此行是构建这样的语句的示例:
(?1 is null or ?1 = '' or (p.fullname like %?1% and c.borrowerId = p.id))
但是执行时,我遇到了一个错误:Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node
这是我要执行的查询,有人可以解释为什么代码出现上面的错误吗?如果设置了nativeQuery = true
,则查询可以成功执行,但如果尝试使用HQL语法,则查询将出错。
以下是查询:
@Query(value = "select c from Contract c, Profile p where \n" +
"(?1 is null or ?1 = '' or (p.fullname like %?1% and c.borrowerId = p.id)) and \n" +
"(?2 is null or ?2 = '' or (p.phone like (select phone where p.phone like %?2% and p.id = c.borrowerId group by p.phone))) \n" +
"and (?3 is null or ?3 = '' or c.createdDate >= ?3) and (?4 is null or ?4 = '' or c.createdDate <= ?4) " + "and (?5 is null or ?5 = '' or c.status = ?5) ")
Page<Contract> findContractsWithOptionalParameters(String name, String phone, Date startDate, Date endDate, String status, Pageable pageable);