to-datetime

dy2hfwbg  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(275)

我在我的项目中使用带hibernate的springboot。我想知道是否可以在hibernate查询中强制转换date和time-to-datetime/timestamp。
有什么问题?我有“term”表:

id_term|date      |time    |id_appointment|accepted|
-------|----------|--------|--------------|--------|
     45|2020-10-22|11:30:00|             6|       1|
   4247|2020-10-19|19:00:00|             4|       1|
  62648|2020-11-02|08:00:00|              |       0|
  62649|2020-11-02|08:30:00|              |       0|

如你所见,这里有一列 date 以及 time . 我想从当前的“日期时间”中获得下一个25个自由期限(自由期限是指 id_appointment 为空)。目前我有这样的疑问: SELECT TOP 25 * FROM Term WHERE id_appointment IS NULL AND time > :currentTime AND date > :currentDate . 但它不能正常工作。例如,如果现在是下午2:15,明天我在上午11点到下午4点之间有自由期,那么这个查询只在下午2:15之后返回自由期。我正在寻找类似的解决方案: SELECT TOP 25 * FROM Term WHERE id_appointment IS NULL AND CAST(date + time) > :currentDateTime . 田野 Term java中的类是 LocalDate 以及 LocalTime 变量。

whlutmcx

whlutmcx1#

我解决了我的问题。正确答案是:

@Query(value = "SELECT TOP 20 * FROM term WHERE id_appointment IS NULL AND (CAST(date as datetime) + CAST(time as datetime)) > :currentDateTime ORDER BY date ASC, time ASC", nativeQuery=true)
List<Term> getNext20FreeTerms(@Param("currentDateTime") LocalDateTime currentDateTime);

相关问题