Java 使用Spring@query运行JPQL查询时发生异常
它抛出异常,如Java 使用Spring@query运行JPQL查询时发生异常,java,hibernate,jpa,spring-data-jpa,Java,Hibernate,Jpa,Spring Data Jpa,它抛出异常,如org.hibernate.hql.internal.ast.QuerySyntaxException:预期关闭,在第1行附近发现“day”。JPA不理解此MySQL函数。 因此,为了解决您的问题,您应该计算查询之外的间隔,并将间隔作为参数提供给查询。 在您的情况下,您应该设法做到这一点,因为间隔不依赖于请求表中的任何数据,而是依赖于执行查询之前的已知数据,因为您使用:startDate和:endDate参数计算间隔: @Query("select new map(count(t.
org.hibernate.hql.internal.ast.QuerySyntaxException:预期关闭,在第1行附近发现“day”。
JPA不理解此MySQL函数。
因此,为了解决您的问题,您应该计算查询之外的间隔,并将间隔作为参数提供给查询。在您的情况下,您应该设法做到这一点,因为间隔不依赖于请求表中的任何数据,而是依赖于执行查询之前的已知数据,因为您使用
:startDate
和:endDate
参数计算间隔:
@Query("select new map(count(t.status) as allCount,sum(case when t.status='Approved' then 1 else 0 end) as approvedCount, "
+ "sum(case when t.status='Overdue' then 1 else 0 end) as overdueCount,"
+ "sum(case when t.status='Rejected' then 1 else 0 end) as rejectedCount,"
+ "sum(case when t.status='Awaiting Approval' then 1 else 0 end) as awaitingApprovalCount,"
+ "sum(case when t.status='Not Submitted' then 1 else 0 end) as notSubmittedCount) "
+ "from Timesheet as t where t.emplId=:employeeId and (t.startDate between date_add(:startDate, interval -6 day) and date_add(:endDate,interval 6 day))"
+ "and (t.endDate between date_add(:startDate, interval -6 day) and date_add(:endDate,interval 6 day))")
在调用查询的方法中,计算日期(我使用了JodaTime作为示例):
在查询中,用这些参数化日期替换MySql函数
Date computedStartDate = new DateTime(startdate).minusDay(6).toDate();
Date computedEndDate = new DateTime(endDate).plusDay(6).toDate();
...
// you create your query
//...
//you set these dates
query.setParameter("computedStartDate",computedStartDate);
query.setParameter("computedEndDate",computedEndDate);
// you execute your query
任何JPQL引用都会告诉您。。。“日期添加”、“间隔”、“日期”是无效的JPQL关键字。JPQL!=SQL
Date computedStartDate = new DateTime(startdate).minusDay(6).toDate();
Date computedEndDate = new DateTime(endDate).plusDay(6).toDate();
...
// you create your query
//...
//you set these dates
query.setParameter("computedStartDate",computedStartDate);
query.setParameter("computedEndDate",computedEndDate);
// you execute your query
@Query("select new map(count(t.status) as allCount,sum(case when t.status='Approved' then 1 else 0 end) as approvedCount, "
+ "sum(case when t.status='Overdue' then 1 else 0 end) as overdueCount,"
+ "sum(case when t.status='Rejected' then 1 else 0 end) as rejectedCount,"
+ "sum(case when t.status='Awaiting Approval' then 1 else 0 end) as awaitingApprovalCount,"
+ "sum(case when t.status='Not Submitted' then 1 else 0 end) as notSubmittedCount) "
+ "from Timesheet as t where t.emplId=:employeeId and (t.startDate between :computedStartDate and :computedEndDate)"
+ "and (t.endDate between :computedStartDate and :computedEndDate)")