我需要按日期查找实体,因此我在JpaRepository
的方法上写了这个查询注释:
@Query(
"select distinct e " +
"from SomeEntity e " +
"join e.someChild c " +
"where e.someField IN :types " +
"and " +
"case " +
"when e.someDate is null then (:month = 12 and :day = 31) " +
"else (month(e.someDate) = :month and day(e.someDate) = :day) " +
"end"
)
List<SomeEntity> findSomeEntities(@Param("month") int monthValue, @Param("day") int dayOfMonth, @Param("types") Set<SomeSubclass> allowed);
Intellij指示else
和(
之间的编译错误:'(', <expression>, <operator>, END, FUNCTION or identifier expected, got '('
。编译时会发生以下情况:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: case near line 1, column 169
编辑:
这是完整的匿名查询:
@Query(
//@formatter:off
"select distinct g " +
"from SomeEntity g " +
"join g.chileOne n " +
"join g.childTwo l " +
"join n.childThree ga " +
"where g.someEntityTyp.typ IN :types " +
"and " +
"case " +
"when l.someDate is null then (:month = 12 and :day = 31) " +
"else (month(l.someDate) = :month and day(l.someDate) = :day) " +
"end"
//@formatter:on
)
List<SomeEntity> findSomeEntity(@Param("month") int monthValue, @Param("day") int dayOfMonth, @Param("types") Set<SomeEntityTyp.Typ> relevantSomeEntityTypes);
错误指向case
关键字的开头。
当someDate
不为null时,此方法工作正常:
@Query(
//@formatter:off
"select distinct g " +
"from SomeEntity g " +
"join g.chileOne n " +
"join g.childTwo l " +
"join n.childThree ga " +
"where g.someEntityTyp.typ IN :types " +
"and month(l.someDate) = :month " +
"and day(l.someDate) = :day")
//@formatter:on
)
List<SomeEntity> findSomeEntity(@Param("month") int monthValue, @Param("day") int dayOfMonth, @Param("types") Set<SomeEntityTyp.Typ> relevantSomeEntityTypes);