java - Spring JPA - unexpected AST node
问题描述
I started a project to learn Spring Data JPA. This query is to retrieve all Contract
that are associated with a Profile in the database.
The requirement is that the search criteria needs to be dynamic: there could be either empty, null or a value, this line is an example to build such a statement:
(?1 is null or ?1 = '' or (p.fullname like %?1% and c.borrowerId = p.id))
But when execute, I ran into an error: Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node
This is the query that I'm trying to execute, could someone explain why the code got the error above? The query can execute successfully if I set nativeQuery = true
, but error if I try to use HQL syntax.
Here is the query:
@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);
解决方案
推荐阅读
- android - 无论如何,有没有通过 Appium 为 IOS 和 android 自动扫描二维码?
- angular - 如何使用带有两个 html 的 routerLink
- r - 如何循环一列,如果模式匹配,使用 R 计算其他列中的相关字符?
- javascript - Google Scripts 中的简单计数功能
- python - 如何使用字符串列表作为变量名/对象属性?
- python - Discord.py bot 输出到文件 python
- typescript - 开玩笑测试随机失败
- github - 来自私人 github 存储库的 Pip install wheel
- typescript - 给定一个类型,定义一个函数的联合,参数为该类型的键
- python-3.x - TypeError:必须是实数,而不是无类型加上更多错误