如何编写 JPA 查询来查找两个日期之间的对象?

发布于 2025-01-09 01:05:48 字数 531 浏览 0 评论 0原文

这是我的查询

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

救星 2025-01-16 01:05:49
List<Statement> findByDateBetween(Date start, Date end);
List<Statement> findByDateBetween(Date start, Date end);
青瓷清茶倾城歌 2025-01-16 01:05:49

事实证明这是正确的实施

@Query(value = "SELECT * FROM account.statement WHERE date > ?1 AND date <= ?2",
nativeQuery = true)
List<Statement> findAllByDate(LocalDate startDate, LocalDate endDate);

谢谢我的家伙托马斯

Turns out this was the correct implementation

@Query(value = "SELECT * FROM account.statement WHERE date > ?1 AND date <= ?2",
nativeQuery = true)
List<Statement> findAllByDate(LocalDate startDate, LocalDate endDate);

Thank you my guy Thomas

爱你是孤单的心事 2025-01-16 01:05:49

虽然使用 @Query 可能会解决问题,但在这种情况下您也可以使用 JpaRepository

  1. 首先您需要将 JpaRepository 添加到您的存储库
    接口:

    扩展 JpaRepository<语句,长>

  2. 使用此行并将 Date 替换为日期列名称并使用适当的日期类型:

    列出 findByDateBetween(LocalDateTime to, LocalDateTime
    来自);

例如:

    @Repository 
public interface StatementRepository  extends JpaRepository<Statement, Long> {
     
     //where x.startDate between ?1 and ?2
     List<Statement> findByDateBetween(LocalDate to,LocalDate from);
     
     }

有关更多信息,您可以查看 doc< /a>

while using @Query might solve the problem, but you can also use JpaRepository in this case

  1. First of all you need to add JpaRepository to your repository
    interface :

    extends JpaRepository<Statement, Long>

  2. 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 :

    @Repository 
public interface StatementRepository  extends JpaRepository<Statement, Long> {
     
     //where x.startDate between ?1 and ?2
     List<Statement> findByDateBetween(LocalDate to,LocalDate from);
     
     }

for more information you can see the doc

蹲在坟头点根烟 2025-01-16 01:05:49

您应该将 LocalDate(而不是 String)绑定到占位符:

@Query(value = " SELECT * FROM account.statement WHERE date BETWEEN ?1 and ?2")
List<Statement> findAllByDate(LocalDate startDate, LocalDate endDate);

You should be binding LocalDate, not String, to the placeholders:

@Query(value = " SELECT * FROM account.statement WHERE date BETWEEN ?1 and ?2")
List<Statement> findAllByDate(LocalDate startDate, LocalDate endDate);
梦幻的心爱 2025-01-16 01:05:49
@Query("select a from Statement a where a.startDate >= :startDate and a.endDate <= :enddate")
List<Statement> findAllByDate(
@Param("startDate") Date startDate,@Param("endDate") Date endDate);
@Query("select a from Statement a where a.startDate >= :startDate and a.endDate <= :enddate")
List<Statement> findAllByDate(
@Param("startDate") Date startDate,@Param("endDate") Date endDate);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文