首页 > 解决方案 > 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);

标签: javahibernatespring-data-jpahqljpql

解决方案


推荐阅读