java - 在休眠查询中将日期和时间转换为 DateTime
问题描述
我在我的项目中使用带有休眠功能的弹簧启动。我想知道是否可以在休眠查询中将日期和时间转换为日期时间/时间戳。
问题是什么?我有表`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
为 NULL)。目前我有这样的查询: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
变量。
解决方案
我解决了我的问题。正确答案是:
@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);
推荐阅读
- regex - 清漆 modsecurity 规则语法错误
- dompdf - Laravel Dompdf 两列布局
- c# - 更改 ItemDatabound 上数据列表内的链接按钮前景色
- java - 无法将 2020 年 4 月 15 日上午 12:14:17 的日期解析为 LocalDatetime
- hadoop - 在将模型部署到 AKS PipelineModel.load 时抛出 org.apache.hadoop.mapred.InvalidInputException
- spring - 我如何解决 Line xsi:schemalocation 中的这个错误
- pgbadger - pgbadger 在 Fedora 上没有找到任何查询
- xamarin - 有什么方法可以将 UIKit 命名空间添加到 Xamarin 表单共享项目?
- python - 是否有可能在 Python GUI 的类中定义全局变量?
- java - 返回列表的房间查询