Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要一个JPA查询,对过去4小时发布的帖子进行排序_Java_Spring_Spring Boot_Jpa_Jpql - Fatal编程技术网

Java 需要一个JPA查询,对过去4小时发布的帖子进行排序

Java 需要一个JPA查询,对过去4小时发布的帖子进行排序,java,spring,spring-boot,jpa,jpql,Java,Spring,Spring Boot,Jpa,Jpql,@存储库 公共接口PostRepository扩展了JpaRepository{ Page<Post> findAllByCategory(Pageable pageable, Category category); LocalDate dateBefore4Hours = LocalDate.now().minus(4, ChronoUnit.HOURS); //tried but couldnt figured it out @Query("SELECT a FROM P

@存储库 公共接口PostRepository扩展了JpaRepository{

Page<Post> findAllByCategory(Pageable pageable, Category category);


LocalDate dateBefore4Hours = LocalDate.now().minus(4, ChronoUnit.HOURS); //tried but couldnt figured it out


@Query("SELECT a FROM Post a WHERE a.createdDate  ") //none of sql keywords i've tried here didnt wor
Page<Post> findAllWithCreationDateTimeInLastFourHours();
Page findAllByCategory(可分页、可分页、类别);
LocalDate dateBefore4Hours=LocalDate.now().minus(4,ChronoUnit.HOURS);//尝试过,但无法解决
@Query(“SELECT a FROM a WHERE a.createdDate”)//我在这里尝试过的sql关键字都不起作用
页面findAllWithCreationDateTimeInLastFourHours();

}

如果需要比较时间,可以使用
LocalDateTime
,因为
LocalDate
没有时间组件。因此,要获取4小时前的
LocalDateTime
,可以执行以下操作:

LocalDateTime CurrentTimeMins4Hours=LocalDateTime.now().MinsHours(4L);
然后,您可以在查询中使用它并对列进行排序,如下所示:

@Query(“从a.createdDate>:currentTimeMinus4Hours ORDER BY a.createdDate中选择一个帖子”)
Page findRecentPosts(LocalDateTime CurrentTime最少4小时);

顺便说一句,我是新手。太多了,我想它奏效了。因此,我假设当我定义一个方法时,我可以使用“:colon:”right?与jpql一起使用它。是的,您可以在jpql查询中使用“:argument”引用方法参数。如果答案解决了问题,请记住将其标记为正确答案。谢谢