WHERE CLAUSE问题中的JPQL案例陈述

时间:2017-02-06 14:27:50

标签: java mysql hibernate jpa jpql

我在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);

此处是日志中的错误:

  

引起:org.hibernate.hql.internal.ast.QuerySyntaxException:   意外的AST节点:靠近第1行,第589行 [从中选择e   com.itengine.bettinggateway.dao.EventTeamPlayer etp join etp.event e   left join e.leagueTournament lt left left lt.sportCategory sc left   加入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&gt; =:startDate和e.startDate&lt; =:endDate)和   (lt.defaultName like:searchTerm或s.name like:searchTerm或   htpl.name喜欢:searchTerm或atpl.name,如:searchTerm或e.id之类的   :searchTerm)和(当(:minDate不为空且:maxDate不为空时)   null)然后(e.startDate&gt; =:minDate和e.startDate&lt; =:maxDate)否则   true end)= true和lt.id =:leagueTournamentId]

2 个答案:

答案 0 :(得分:2)

我认为你不能在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)                   
)

答案 1 :(得分:1)

如果你看一下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

你在你的子句中违反了这一点。尝试用AND-OR组合替换你的CASE。