spring boot repository: check if Date exists

1.2k Views Asked by At

In my reservation-entity i have a column "bookingDate" --> example: "2021-05-10 12:00:00".

So in this object the date and starttime of an user-booking gets displayed.

If a user wants to book a timeslot, i want to check first if the selected timeslot is empty. So i want to query the database by date&startTime.

I tried it with https://www.baeldung.com/spring-data-jpa-query-by-date , but it didnt work. I got the errors: "The annotation @Temporal is disallowed for this location" & "@Temporal cant be used for variables"

these are the relevant classes:

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

With the method "findByBookingDate()" i want to query the database, if the selected timeslot is empty...

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);

}

If I run it like this i always get an "org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'backyardcodersSpringReactApplication'" --> so the application does not successfully start up.

Im very thankful for every tip and critique!

cheers!

4

There are 4 best solutions below

0
On

I copied your code in my local file. Instead of

import org.springframework.data.jpa.repository.Temporal;

I used

import javax.persistence.Temporal;

in your Reservation.java file.

Also this is my very first answer on Stackoverflow.

0
On

First of all @Temporal have three different arguments and it can be applied to the variable of type date , time and time stamp.

Usage

@Temporal(Temporal type.DATE)
private Date date;


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

Not understood completely. this is just a lead maybe not a perfect solution.

  1. You can use java.time.LocalDateTime . and annotation be like @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME).

  2. And the query should be like. [the query will check all the reservation for that day]

Select from reservation_table Where timeSlot between ‘2021-05-10 00:00:00’ and ‘2021-05-10 23:59:59’

0
On

Why not just just extract the localdate from your LocalDateTime and pass it on? and extract the hour and pass it on and query 2 different columns with it.

LocalDateTime.toLocalDate()
LocalDateTime.getHour()