0

I'm developing java application with JPA. In the application I have Rooms and Reservations :

@Entity
@Table(name = "RESERVATIONS")
public class Reservation {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @ManyToOne
    private Room room;
    private LocalDate checkInDate;
    private LocalDate checkOutDate;

    //getters setters
}

I want to validate that the room cannot be booked twice for the same period.

Any ideas how I can do that?

Petar Petrov
  • 586
  • 2
  • 10
  • 28
  • 3
    You'll need to write code. That's business logic. Execute a query checking if the room is already checked in that period before creating the reservation. – JB Nizet Sep 02 '17 at 17:26
  • Yes I thought soo but does this solution would be thread safe if I put the both queries in one transaction? – Petar Petrov Sep 02 '17 at 18:16
  • 1
    No, it wouldn't. But two reservations of the same room in such a short timespan is very unlikely, and you could add other ways to avoid that situation (like, for example, a regular check of the validity of each room, and compensating measures, or locks). – JB Nizet Sep 02 '17 at 18:20
  • short transaction on finalisation (in business level: operator decision to finalise) seems good idea, with lock on Room. JPA 2.0 has good one – Jacek Cz Sep 02 '17 at 18:29

0 Answers0