【发布时间】: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