java.lang.Object
eus.ehu.hotelreservation.dataAccess.DataAccess
Data-access layer built on Hibernate / JPA with an embedded H2 database.
Manages the EntityManager lifecycle and exposes CRUD operations
for the domain entities (Hotel, Room,
Reservation, WaitingListEntry, etc.).
Supports an initialize mode that drops and recreates the schema
with test data on startup.
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes theEntityManagerand releases database resources.getAvailableRooms(String hotelName, LocalDate startDate, LocalDate endDate) Returns available room types (not specific rooms) for a hotel and date range.getCustomer(String nationalId) Looks up a customer by national ID.Retrieves all hotels from the database.voidInitializes the database schema and populates it with sample data.voidjoinWaitingList(String hotelName, String roomTypeStr, LocalDate startDate, LocalDate endDate, Customer customer) Adds a customer to the waiting list for a room type at a hotel.voidopen()Opens a connection to the database using the configuration defined inhibernate.cfg.xmlandconfig.properties.voidopen(boolean initializeMode) voidreserveRoom(AvailableRoomModel room, LocalDate startDate, LocalDate endDate, Customer customer) Reserves a room of the given type at the given hotel for the date range.voidreset()
-
Field Details
-
db
protected jakarta.persistence.EntityManager db -
emf
protected jakarta.persistence.EntityManagerFactory emf
-
-
Constructor Details
-
DataAccess
public DataAccess()
-
-
Method Details
-
open
public void open()Opens a connection to the database using the configuration defined inhibernate.cfg.xmlandconfig.properties.Equivalent to calling
open(boolean)withfalse. -
open
public void open(boolean initializeMode) -
reset
public void reset() -
close
public void close()Closes theEntityManagerand releases database resources. -
initializeDB
public void initializeDB()Initializes the database schema and populates it with sample data.This method is intended to be called once at application startup when
db.openmode=initializeinconfig.properties. It creates room types, hotels, rooms, rates and test customers inside a single JPA transaction.- See Also:
-
getHotels
-
getCustomer
-
getAvailableRooms
public List<AvailableRoomModel> getAvailableRooms(String hotelName, LocalDate startDate, LocalDate endDate) Returns available room types (not specific rooms) for a hotel and date range. Only room types that have at least one available room for ALL days in the range are returned. For each available room type, it calculates the total price across all days (considering different rates on different dates). -
reserveRoom
public void reserveRoom(AvailableRoomModel room, LocalDate startDate, LocalDate endDate, Customer customer) Reserves a room of the given type at the given hotel for the date range. The customer pays 20% upfront via credit card. -
joinWaitingList
-