【发布时间】:2021-09-07 01:39:54
【问题描述】:
我必须编写一个查询来计算两个日期之间的年时差。其中一个日期是从数据库中获取的,而另一个是 LocalDate。以下是我写的代码:
Expression<String> year = new UnitExpression(null, String.class, "YEAR");
LocalDate date=LocalDate.of(2018, 04, 30);
Expression<Integer> timeDiff = builder.function(
"TIMESTAMPDIFF",
Integer.class,
year ,
propertyListRoot.get("rentStartDate"),
date);
其中 UnitExpression 是扩展 BasicFunctionExpression 的自定义类。但是,这会产生编译时错误提示
必需的类型表达式>提供的LocalDate
我研究了一下并尝试了
criteriaBuilder.literal(日期)
这解决了编译时错误,但不会计算正确的值。什么是可能的解决方案?我在这里错过了什么?
【问题讨论】:
标签: mysql spring-boot spring-data-jpa hibernate-criteria timestampdiff