如何编写 JPA 查询来查找两个日期之间的对象?
这是我的查询
@Query(value = " SELECT * FROM account.statement where `date` between ?1 and ?2")
List<Statement> findAllByDate(String startDate, String endDate);
这是我收到的错误消息
Caused by: org.hibernate.QueryException: unexpected char: '`' [ SELECT * FROM account.statement where `date` between ?1 and ?2]
date
是列名称。我想检索两个日期之间的 Statement 对象列表。本例中的日期是 LocalDate 对象,例如 2000-10-10。我尝试在参数中使用 String 和 LocalDate 类型,但仍然不起作用。
我在堆栈溢出和 baeldung 上到处搜索。我被困住了
Here's my query
@Query(value = " SELECT * FROM account.statement where `date` between ?1 and ?2")
List<Statement> findAllByDate(String startDate, String endDate);
And this is the error message I get
Caused by: org.hibernate.QueryException: unexpected char: '`' [ SELECT * FROM account.statement where `date` between ?1 and ?2]
The date
is the column name. I want to retrieve a list of Statement objects between two dates. The dates in this case are LocalDate objects, like 2000-10-10. I tried using String and a LocalDate type in the parameters, still doesn't work.
I've searched everywhere, on stack overflow and baeldung. I am stuck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
事实证明这是正确的实施
谢谢我的家伙托马斯
Turns out this was the correct implementation
Thank you my guy Thomas
虽然使用 @Query 可能会解决问题,但在这种情况下您也可以使用 JpaRepository
首先您需要将 JpaRepository 添加到您的存储库
接口:
扩展 JpaRepository<语句,长>
使用此行并将 Date 替换为日期列名称并使用适当的日期类型:
列出 findByDateBetween(LocalDateTime to, LocalDateTime
来自);
例如:
有关更多信息,您可以查看 doc< /a>
while using @Query might solve the problem, but you can also use JpaRepository in this case
First of all you need to add JpaRepository to your repository
interface :
extends JpaRepository<Statement, Long>
use this line and replcae Date with the date column name and use the appropriate type of date:
List findByDateBetween(LocalDateTime to, LocalDateTime
from);
exemple :
for more information you can see the doc
您应该将
LocalDate
(而不是String
)绑定到占位符:You should be binding
LocalDate
, notString
, to the placeholders: