检查jpql查询中的java时间戳是否为null



我有以下代码:

@Query("select t from Training t join t.skills s join t.trainers tr join t.discipline d  where " +
"(t.name in :names or :names is null) and (s.name in :skills or :skills is null) and" +
" (t.location = :location or :location is null) and " +
" (d.name = :discipline or :discipline is null) and " +
"(tr.firstName in :trainers or :trainers is null) and " +
" (((:endDate > t.endDate) and (:startDate < t.startDate)) or (:startDate is empty))")
public List<Training> filterTrainings(List<String> names, List<String> skills, String location,String discipline,List<String> trainers,Timestamp endDate,Timestamp startDate);

并且我需要检查CCD_ 1和CCD_。有办法做到这一点吗?

我得到的错误是nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

当尝试检查:startDate is null时,其中开始日期是时间戳。

您可以尝试将LocalDateTime作为参数而不是时间戳来传递吗?这里的问题可能是java.sql.Timestamp引起的。您可以通过调用timestamp.toLocalDateTime((转换为LocalDateTime

或者,您可以尝试将时间戳作为字符串传递到filterTrainings中。如果在调用filterTrainings方法之前时间戳为null,请指定一个空字符串。String _timestamp = timestamp == null ? "" : timestamp.toString()

然后,在sql语句中检查字符串是否为空。。""=:timestamp OR function("to_timestamp", :timestamp, "yyyy-mm-dd hh:mm:ss.fffffffff")。这里的问题是,我们使用函数来访问本机db命令。

也许您可以使用and COALESCE(field, default_value_for_datetime_field)构建SQL查询。因此,您不会得到NULL值。

您可以将默认值构建为当前月份的开始、当前周的开始,或者任何可以公平替代模型域的日期时间值。

相关内容

最新更新