JPQL CASE 语句中的 WHERE 子句问题
JPQL CASE STATEMENT in WHERE CLAUSE PROBLEMS
我在 where
子句中的 case 语句有一些问题。如果有人知道如何解决这个问题,请帮助我!谢谢。
@Query("select e from EventTeamPlayer etp "
+ "join etp.event e "
+ "left join e.leagueTournament lt "
+ "left join lt.sportCategory sc "
+ "left join e.sport s "
+ "left join etp.homeTeamPlayer htpl "
+ "left join etp.awayTeamPlayer atpl "
+ "left join lt.country c "
+ "left join e.eventStatus es "
+ "where (e.startDate >= :startDate and e.startDate <= :endDate) "
+ "and lt.id = :leagueTournamentId "
+ "and (lt.defaultName like :searchTerm or "
+ "s.name like :searchTerm or "
+ "htpl.name like :searchTerm or "
+ "atpl.name like :searchTerm or "
+ "e.id like :searchTerm) and "
+ "(case when (:minDate is not null and :maxDate is not null) "
+ " then (e.startDate >=:minDate and e.startDate<=:maxDate) else true end) = true")
Page<Event> getEventsForWebAdmin(Pageable pageable,
@Param("searchTerm") String searchTerm,
@Param("leagueTournamentId") int leagueTournamentId,
@Param("startDate") Date startDate,
@Param("endDate") Date endDate,
@Param("minDate") Date minDate,
@Param("maxDate") Date maxDate);
这是日志中的错误:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException:
unexpected AST node: and near line 1, column 589 [select e from
com.itengine.bettinggateway.dao.EventTeamPlayer etp join etp.event e
left join e.leagueTournament lt left join lt.sportCategory sc left
join e.sport s left join etp.homeTeamPlayer htpl left join
etp.awayTeamPlayer atpl left join lt.country c left join e.eventStatus
es where (e.startDate >= :startDate and e.startDate <= :endDate) and
(lt.defaultName like :searchTerm or s.name like :searchTerm or
htpl.name like :searchTerm or atpl.name like :searchTerm or e.id like
:searchTerm) and (case when (:minDate is not null and :maxDate is not
null) then (e.startDate >=:minDate and e.startDate<=:maxDate) else
true end) = true and lt.id = :leagueTournamentId]
我不认为你可以在 JPQL 中提取那种语句。
尝试用这样的东西替换它:
AND
(
(:minDate is not null and :maxDate is not null
and e.startDate >=:minDate and e.startDate<=:maxDate)
OR
(:minDate is null or :maxDate is null)
)
如果您查看 JPQL API docs,它表示:
when_clause::= WHEN conditional_expression THEN scalar_expression
所以then
子句可以使用:
scalar_expression ::= arithmetic_expression | string_primary | enum_primary | datetime_primary | boolean_primary | case_expression | entity_type_expression
并且您在 then 子句中违反了这一点。尝试用 AND-OR 组合替换您的 CASE。
我在 where
子句中的 case 语句有一些问题。如果有人知道如何解决这个问题,请帮助我!谢谢。
@Query("select e from EventTeamPlayer etp "
+ "join etp.event e "
+ "left join e.leagueTournament lt "
+ "left join lt.sportCategory sc "
+ "left join e.sport s "
+ "left join etp.homeTeamPlayer htpl "
+ "left join etp.awayTeamPlayer atpl "
+ "left join lt.country c "
+ "left join e.eventStatus es "
+ "where (e.startDate >= :startDate and e.startDate <= :endDate) "
+ "and lt.id = :leagueTournamentId "
+ "and (lt.defaultName like :searchTerm or "
+ "s.name like :searchTerm or "
+ "htpl.name like :searchTerm or "
+ "atpl.name like :searchTerm or "
+ "e.id like :searchTerm) and "
+ "(case when (:minDate is not null and :maxDate is not null) "
+ " then (e.startDate >=:minDate and e.startDate<=:maxDate) else true end) = true")
Page<Event> getEventsForWebAdmin(Pageable pageable,
@Param("searchTerm") String searchTerm,
@Param("leagueTournamentId") int leagueTournamentId,
@Param("startDate") Date startDate,
@Param("endDate") Date endDate,
@Param("minDate") Date minDate,
@Param("maxDate") Date maxDate);
这是日志中的错误:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: and near line 1, column 589 [select e from com.itengine.bettinggateway.dao.EventTeamPlayer etp join etp.event e left join e.leagueTournament lt left join lt.sportCategory sc left join e.sport s left join etp.homeTeamPlayer htpl left join etp.awayTeamPlayer atpl left join lt.country c left join e.eventStatus es where (e.startDate >= :startDate and e.startDate <= :endDate) and (lt.defaultName like :searchTerm or s.name like :searchTerm or htpl.name like :searchTerm or atpl.name like :searchTerm or e.id like :searchTerm) and (case when (:minDate is not null and :maxDate is not null) then (e.startDate >=:minDate and e.startDate<=:maxDate) else true end) = true and lt.id = :leagueTournamentId]
我不认为你可以在 JPQL 中提取那种语句。
尝试用这样的东西替换它:
AND
(
(:minDate is not null and :maxDate is not null
and e.startDate >=:minDate and e.startDate<=:maxDate)
OR
(:minDate is null or :maxDate is null)
)
如果您查看 JPQL API docs,它表示:
when_clause::= WHEN conditional_expression THEN scalar_expression
所以then
子句可以使用:
scalar_expression ::= arithmetic_expression | string_primary | enum_primary | datetime_primary | boolean_primary | case_expression | entity_type_expression
并且您在 then 子句中违反了这一点。尝试用 AND-OR 组合替换您的 CASE。