【问题标题】:spring boot repository: check if Date existsspring boot 存储库:检查日期是否存在
【发布时间】:2021-07-31 17:57:12
【问题描述】:

在我的预订实体中,我有一列“bookingDate”--> 示例:“2021-05-10 12:00:00”。

所以在这个对象中显示了用户预订的日期和开始时间。

如果用户想要预订时间段,我想先检查所选时间段是否为空。所以我想按日期和开始时间查询数据库。

我用 https://www.baeldung.com/spring-data-jpa-query-by-date 试过了,但没用。我收到错误消息:“此位置不允许使用注释 @Temporal”和“@Temporal 不能用于变量”

这些是相关的类:

Reservation.java

@Entity
public class Reservation {

    @Id @GeneratedValue
    private int reservationId;
    
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private LocalDateTime bookingDate;

    private int court = 1;
    

    
    private String playerNames;
    private int userIdReservation;

    //getter and setters

使用“findByBookingDate()”方法我想查询数据库,如果选择的时间段为空...

VerificationClass.java

        public boolean validateReservation(Reservation r) {
        LocalDateTime tempDate = r.getBookingDate();
        if(reservationRepository.findByBookingDate(tempDate)){ // todo: + and Court
            logger.getLogger().info(this.getClass().getName() + "||Booking Slot is empty -- Reservation created||");
            return true;
        }
        logger.getLogger().info(this.getClass().getName() + "||Booking Slot is full -- Reservation failed||");
        return false;
    }

ReservationRepository.java

@Repository
@Repository
public interface ReservationRepository extends JpaRepository<Reservation, Integer>{

    
@Query("Select r from reservation r where r.booking_date = :tempDate")
     boolean findByBookingDate(@Param("tempDate") LocalDateTime tempDate);

}

如果我像这样运行它,我总是会得到一个“org.springframework.beans.factory.UnsatisfiedDependencyException: Error created bean with name 'backyardcodersSpringReactApplication'”--> 所以应用程序没有成功启动。

我非常感谢每一个提示和批评!

干杯!

【问题讨论】:

  • 你应该在你的 Reservation 实体中使用 java.util.Date
  • 它也不起作用 --> 错误:“此位置不允许使用注释 @Temporal”

标签: java mysql spring-boot date find-by-sql


【解决方案1】:

没有完全理解。这只是一个线索,也许不是一个完美的解决方案。

  1. 您可以使用 java.time.LocalDateTime 。和注释就像@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)。

  2. 查询应该是这样的。 [查询将检查当天的所有预订]

选择 从预订表 在哪里 “2021-05-10 00:00:00”和“2021-05-10 23:59:59”之间的时隙

【讨论】:

  • 嘿,谢谢你的回答。另一个问题是可以这样做:@Query("Select r from reservation r where r.booking_date = :tempDate") boolean findByBookingDate(@Param("tempDate") LocalDateTime tempDate); 还是我需要像您建议的那样搜索时间跨度。以便我获得当天所有预订的清单?
  • 我更新了我的帖子,也许你现在明白了:)
  • @judo_ürgens,当您预订一个空位时,您可能需要检查 reservationFromDateTime 和 reservationToDateTime 以便您可以获得实际的空位。
  • 查询应该是这样 select * from reservation_table where Date(booking_date) = :reservationRequestDate; // 即 '2021-05-10' 如果查询返回结果,则不预订该插槽,否则预订它
【解决方案2】:

我将您的代码复制到我的本地文件中。而不是

导入 org.springframework.data.jpa.repository.Temporal;

我用过

导入 javax.persistence.Temporal;

在您的 Reservation.java 文件中。

这也是我在 Stackoverflow 上的第一个答案。

【讨论】:

    【解决方案3】:

    首先@Temporal具有三个不同的参数,可以应用于日期、时间和时间戳类型的变量。

    用法

    @Temporal(Temporal type.DATE)
    private Date date;
    
    
    @Temporal(Temporal type.TIMESTAMP) // INSERT BOTH DATE WITH TIME TILL MILLISECONDS
    private Calendar date;
    

    【讨论】:

      【解决方案4】:

      为什么不直接从 LocalDateTime 中提取 localdate 并将其传递?并提取小时并将其传递并使用它查询 2 个不同的列。

      LocalDateTime.toLocalDate()
      LocalDateTime.getHour()
      

      【讨论】:

        猜你喜欢
        • 2020-04-01
        • 1970-01-01
        • 2018-10-14
        • 2021-04-16
        • 2011-09-08
        • 1970-01-01
        • 1970-01-01
        • 2021-11-07
        • 2014-04-22
        相关资源
        最近更新 更多