增加X小时-@Query-Spring Data JPA [英] Adding X hours - @Query - Spring Data JPA

查看:86
本文介绍了增加X小时-@Query-Spring Data JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过在Spring Data JPA存储库中采用另一种方法,该方法应将给定的小时数添加到时间戳中.

I have tried make a different method in Spring Data JPA Repository which should adding the given hours to timestamp.

public interface ActivationCodeRepository extends CrudRepository<ActivationCode, Long> {

  @Query(value = "select a from ActivationCode a where a.creationTime + INTERVAL '1 hour' * :hoursAgo  <=  CURRENT_TIMESTAMP and a.type = :type and a.account = account")
  List<ActivationCode> getAllAddedAtLeastXHoursAgo(@Param("account") Account account, @Param("type") int type, @Param("hoursAgo") int hoursAgo);


}

由于该原因它不起作用:

It is not working because of it:

  • 间隔'1小时'*:hoursAgo

完全是:

"1小时"

IDE下划线并给出以下错误:

The IDE underscores and given this error:

<'expression'>,<'operator'>,GROUP,HAVING或ORDER.

<'expression'>, <'operator'>, GROUP, HAVING or ORDER expected.

我尝试做一些研究,发现我应该如何精确地将给定的小时数添加到creationTime中,但是找不到任何地方.

I have tried do some research and find how exactly I should adding the given hours to the creationTime but not found anywhere.

推荐答案

类似于 a_horse_with_no_name 说我必须使用本机查询来做类似的事情,所以我将方法更改为:

Like a_horse_with_no_name said I had to use native query to do something like that so I changed my method to:

  @Query(value = "select * from activation_codes a where a.type = :type and a.id_account = :id_account and a.creation_time <= NOW() - :hoursAgo * INTERVAL '1 hour'", nativeQuery = true)
  List<ActivationCode> getAllAddedAtLeastXHoursAgo(@Param("id_account") int id_account, @Param("type") int type, @Param("hoursAgo") int hoursAgo);

它正常工作.感谢您的所有帮助.

And it works correct. Thanks for all help.

这篇关于增加X小时-@Query-Spring Data JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆