Spring 使用postgresql的daterange的JPA查询

Spring 使用postgresql的daterange的JPA查询,spring,postgresql,spring-boot,spring-data-jpa,Spring,Postgresql,Spring Boot,Spring Data Jpa,实际数据库表: Column | Type | Collation | Nullable | Default --------+-----------+-----------+----------+--------- room | text | | | during | daterange | | | 工作数据库查询: select * from room_reservation wher

实际数据库表:

 Column |   Type    | Collation | Nullable | Default
--------+-----------+-----------+----------+---------
 room   | text      |           |          |
 during | daterange |           |          |
工作数据库查询:

select * from room_reservation where during && daterange('[2020-10-15,2020-11-14)');
实体映射:

@Column(name = "during", columnDefinition = "daterange")
private Range<Date> during;

如何在JPA中编写相同的查询?

因为它是本机查询使用?作为参数占位符:

@Query(value = "select r.room from Room_Occupancy r where r.during && daterange('[?, ?)')", nativeQuery = true)
List<Long> findOccupiedRooms(@Param("d1") Date d1, @Param("d2") Date d2);
@Query(value=“从房间中选择r.room,其中r.during&&daterange(“[?,?)”),nativeQuery=true)
列出FinDocCupiedRoom(@Param(“d1”)日期d1,@Param(“d2”)日期d2);
尝试以下代码:-

    @Query(value = "select r.room from Room_Occupancy r where r.during >= ?1 and r.during < ?2", nativeQuery = true)
    List<String> findOccupiedRooms(Date d1,Date d2); //d1 < d2
@Query(value=“从房间中选择r.room,其中r.during>=?1和r.during<?2”,nativeQuery=true)
列出FinDocCupiedRoom(日期d1,日期d2);//d1
此处,
'[?1,?2)
内部字符串文字参数无法替换。您可以使用
|
将字符串压缩为可以替换参数的字符串

@Query(value = "select r.room from Room_Occupancy r where r.during &&"
      + " daterange('[' || :d1 || ',' || :d2 || ')')", nativeQuery = true)
  List<Long> findOccupiedRooms(@Param("d1") Date d1, @Param("d2") Date d2);
@Query(value=“从房间中选择r.room,其中r.during&&”
+“日期范围('['| |:d1 | |','| |:d2 | |')”,nativeQuery=true)
列出FinDocCupiedRoom(@Param(“d1”)日期d1,@Param(“d2”)日期d2);
@Query(value=“从房间中选择r.room,其中r.during和daterange(“| |”[“| |:d1 | |],“| |:d2 | |”)”,nativeQuery=true)
列出FinDocCupiedRoom(@Param(“d1”)日期d1,@Param(“d2”)日期d2);

这很有效

我不明白为什么需要额外的
'
来连接两侧。我测试了它在没有它的情况下是否有效。你能解释一下你的解决方案吗?
    @Query(value = "select r.room from Room_Occupancy r where r.during >= ?1 and r.during < ?2", nativeQuery = true)
    List<String> findOccupiedRooms(Date d1,Date d2); //d1 < d2
@Query(value = "select r.room from Room_Occupancy r where r.during &&"
      + " daterange('[' || :d1 || ',' || :d2 || ')')", nativeQuery = true)
  List<Long> findOccupiedRooms(@Param("d1") Date d1, @Param("d2") Date d2);
 @Query(value = "select r.room from Room_Occupancy r where r.during && daterange(''||'[' || :d1 ||',' || :d2 ||')' || '')", nativeQuery = true)
        List<Long> findOccupiedRooms(@Param("d1") Date d1, @Param("d2") Date d2);