0

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 Answers4

0

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’

Praveen Shendge
  • 303
  • 3
  • 13
  • Hey thank you for the answer. another question is it possible to do it like this: `@Query("Select r from reservation r where r.booking_date = :tempDate") boolean findByBookingDate(@Param("tempDate") LocalDateTime tempDate);` or do i need to search for a timespan, like you proposed. so that i get a list of all the bookings of that day? – judo_ürgens May 10 '21 at 16:12
  • ive updated my post, maybe you get me now :) – judo_ürgens May 10 '21 at 16:49
  • @judo_ürgens, When you reserve a slot you might want to check reservationFromDateTime and reservationToDateTime so that you can get actual open slot . – Praveen Shendge May 11 '21 at 05:09
  • Query should be like this select * from reservation_table where Date(booking_date) = :reservationRequestDate; // i.e '2021-05-10' if the query returns result don't book the slot otherwise book it – Praveen Shendge May 11 '21 at 05:23
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

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;
paradocslover
  • 2,932
  • 3
  • 18
  • 44
0

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