0

Here is my entity:

 @Entity
@Table(name = "reservation")
public class Reservation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "date_in")
    private LocalDate dateIn;

    @Column(name = "date_out")
    private LocalDate dateOut;

    @ManyToOne
    private Guest guest;

    @ManyToOne
    private Room room;

    public Reservation() {
    }

    public Reservation(LocalDate dateIn, LocalDate dateOut, Guest guest, Room room) {
        this.dateIn = dateIn;
        this.dateOut = dateOut;
        this.guest = guest;
        this.room = room;
    }

    public Guest getGuest() {
        return guest;
    }

    public void setGuest(Guest guest) {
        this.guest = guest;
    }

    public Room getRoom() {
        return room;
    }

    public void setRoom(Room room) {
        this.room = room;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public LocalDate getDateIn() {
        return dateIn;
    }

    public void setDateIn(LocalDate dateIn) {
        this.dateIn = dateIn;
    }

    public LocalDate getDateOut() {
        return dateOut;
    }

    public void setDateOut(LocalDate dateOut) {
        this.dateOut = dateOut;
    }

    @Override
    public String toString() {
        return "Reservation{" +
                "id=" + id +
                ", dateIn=" + dateIn +
                ", dateOut=" + dateOut +
                ", guest=" + guest +
                ", room=" + room +
                '}';
    }
}

Here is my validation method:

@Override public Reservation save(Reservation reservation) { validateDate(reservation.getDateIn(), reservation.getDateOut());

        List<ReservationRepository> bookedRooms = reservationRepository.findBookedRooms();

        if (!bookedRooms.contains(reservation)) {
            return reservationRepository.save(reservation);
        }
        return null;
    }

findBookedRooms() gets this sql query:

@Query(value = "SELECT rs.room_id, rs.date_in, rs.date_out FROM reservation rs LEFT JOIN room r ON rs.room_id = r.id WHERE rs.id IS NOT NULL", nativeQuery = true)
    List<ReservationRepository> findBookedRooms();

PROBLEM: In if statement bookedRooms gets List of:reservation.room_id, reservation.date_in, reservation.date_out parameters, whereas reservation Object gets:reservation.id,reservation.date_in, reservation.date_out. In consequence we've got rs.room_id - rs.id conflict, so "if statement" will always be true.How can I make my reservation object to get these specific parameters:reservation.getRoom().getNumber(), reservation.date_in, reservation.date_out?

I need this validation to check if there is no conflict in dates while booking specific room.

Mr. C
  • 5
  • 2

1 Answers1

0

Your validation consists in using the contains method from the list retrieved from the DB, checking: "the list contains this reservation?".

Your question basically says that the IF statement is always evaluating to true, so it's always evaluating to: "this reservation is not in the list!"

Considering all this, I believe you should check how the contains method in the List interface works. This post has some insight on it.

The contains method uses the equals method to check if a object has an equal object in the list. Since both types used are different (ReservationRepository is not the same as Reservation) it will fail 100% of the time, unless you start overriding the equals method (considering you are using two types for comparison, I would warn you, don't do that).

Possible solution:

Loop through the list and inside the loop check the fields that are needed to evaluate if the Reservation already exists in tour DB. Do it inside your save method, or write another method for better code organization.

The loop would probably look like this:

boolean reservationExists = false;
for(ReservationRepository r:bookedRooms){
     if (r.getRoom().equals(reservation.getRoom()) &&
        r.getDateOut().equals(reservation.getDateOut()) &&
        r.getDateIn().equals(reservation.getDateIn())) {
            reservationExists = true;
            break;
     }
}
Vitor Santos
  • 581
  • 4
  • 15