Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.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/9/spring-boot/5.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查询,以检查是否存在特定年份和月份的记录?_Java_Spring Boot_Jpa_Spring Data Jpa - Fatal编程技术网

Java JPA查询,以检查是否存在特定年份和月份的记录?

Java JPA查询,以检查是否存在特定年份和月份的记录?,java,spring-boot,jpa,spring-data-jpa,Java,Spring Boot,Jpa,Spring Data Jpa,我试图实现一个JPA查询,以检查是否存在任何日期时间戳与当前年份月份匹配的记录。现在我正在获取所有记录并进行迭代以匹配。我知道这不是正确的实现,只是想知道是否有任何内置的JPA查询可用于此场景 这是我到现在为止的实现 List<Product> productList= productRepository.findAll(); /*getMonthInt() and getYearInt() is the custom function which ret

我试图实现一个JPA查询,以检查是否存在任何日期时间戳与当前年份月份匹配的记录。现在我正在获取所有记录并进行迭代以匹配。我知道这不是正确的实现,只是想知道是否有任何内置的JPA查询可用于此场景

这是我到现在为止的实现

     List<Product> productList= productRepository.findAll();

        /*getMonthInt() and getYearInt() is the custom function which returns current 
        month and year. As date.getMonth() and date.getYear() is deprecated
        */

        int currentMonth=getMonthInt(new Date());
        int currentYear=getYearInt(new Date());

        for(Product product:productList){

          int fetchedMonth=getMonthInt(product.getShipmentDate()) ;
          int fetchedYear=getYearInt(product.getShipmentDate());

          if(fetchedMonth == currentMonth && fetchedYear == currentYear){
             //record exist 
          }
          else{
         //do something else
          }
     }

您不需要获取所有记录。如果您试图通过比较时间戳的月份和年份来过滤记录,请遵循以下步骤

方法1:

施工开始日期当年当月的第一天 构造结束日期该年当月的最后一天 使用between关键字并构造一个jpa查询,如下所示 方法2:

使用@query注释构造一个jpa查询,如下所示 您的ProductRepository应该如下所示

public interface ProductRepository extends JpaRepository<Product, Integer> {

    List<Product> findAllByShipmentDateBetween(Date startDate, Date endDate);

    @Query("select p from Product p where year(p.shipmentDate) = ?1 and month(p.shipmentDate) = ?2")
    List<Product> findAllByShipmentDate(Integer year, Integer month);
}
默认情况下,spring data jpa使用基于位置的参数绑定,如第二个查询方法所示,这可能会在维护期间导致问题。可以使用命名参数编写更多可维护的代码。比如说

@Query("select p from Product p where year(p.shipmentDate) = :year and month(p.shipmentDate) = :month")
List<Product> findAllByShipmentDate(@Param("year") Integer year, @Param("month") Integer month);