A Spring Boot application that models core hospital operations and demonstrates robust persistence-layer design with Spring Data JPA and PostgreSQL. The project manages patients, doctors, departments, appointments, and insurance policies, including their relational mappings and transactional service operations.
This application provides a foundation for a hospital-management backend. Its current focus is the domain, repository, and service layers; a REST controller layer has not yet been implemented.
Key capabilities include:
- Patient records with demographic information, blood group, and creation timestamp.
- Doctor records, specializations, and departmental membership.
- Department management, including a department head and doctor assignments.
- Appointment scheduling and reassignment between doctors.
- One-to-one insurance-policy assignment and removal for patients.
- Custom patient queries for blood-group reporting, date filtering, search, paging, bulk updates, and appointment retrieval.
- Database initialization with representative patient, doctor, and appointment data.
| Area | Technology |
|---|---|
| Language | Java 17 |
| Framework | Spring Boot 3.5.16 |
| Data access | Spring Data JPA / Hibernate |
| Database | PostgreSQL |
| Build tool | Maven (Maven Wrapper included) |
| Boilerplate reduction | Lombok |
| Testing | Spring Boot Test / JUnit |
| Entity | Description | Main relationships |
|---|---|---|
Patient |
Stores patient demographic and clinical-reference data. | One insurance policy; many appointments. |
Insurance |
Represents a patient's insurance policy. | One patient. |
Doctor |
Stores a doctor's name, specialization, and email. | Many departments; many appointments. |
Department |
Represents a hospital department. | One head doctor; many doctors. |
Appointment |
Records an appointment time and reason. | One patient; one doctor. |
The Patient entity enforces a unique combination of name and birth date, and uses a string-backed BloodGroupType enum to retain readable blood-group values in the database.
- JDK 17 or later
- PostgreSQL 14 or later
- A PostgreSQL database named
hospitalDB
The default development configuration is located in src/main/resources/application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/hospitalDB
spring.datasource.username=postgres
spring.datasource.password=rootUpdate these values for your local PostgreSQL installation. For shared, staging, or production environments, provide credentials through environment-specific configuration rather than committing them to source control.
The current configuration uses the following development-oriented settings:
spring.jpa.hibernate.ddl-auto=create
spring.sql.init.mode=alwaysddl-auto=create recreates the schema each time the application starts. This is appropriate for local development and demonstrations, but it must be replaced with a safe migration strategy before deploying to an environment that contains persistent data.
-
Create the PostgreSQL database:
CREATE DATABASE hospitalDB;
-
Update the datasource settings in
src/main/resources/application.propertiesif your local PostgreSQL username, password, host, or port differs from the defaults. -
Start the application from the project root:
Windows
mvnw.cmd spring-boot:run
macOS/Linux
./mvnw spring-boot:run
On startup, Hibernate creates the schema and Spring executes src/main/resources/data.sql to load sample patients, doctors, and appointments.
Run the full test suite:
mvnw.cmd testBuild the application artifact:
mvnw.cmd clean packageThe packaged application is created under target/ and can be run with:
java -jar target\hospitalManagement-0.0.1-SNAPSHOT.jarsrc
├── main
│ ├── java/com/kshitijgaikwad/learnspring/hospitalManagement
│ │ ├── entity/ JPA domain entities and enums
│ │ ├── repository/ Spring Data JPA repositories and custom queries
│ │ ├── service/ Transactional business operations
│ │ ├── dto/ Query response objects
│ │ └── HospitalManagementApplication.java
│ └── resources
│ ├── application.properties
│ └── data.sql Development seed data
└── test
└── java/ Application and repository tests
PatientRepository extends JpaRepository and includes derived and JPQL/native queries for common patient workflows:
- Finding patients by name, birth date, email, date range, or blood group.
- Returning blood-group counts through
BloodGroupCountResponseEntity. - Paging patient results with
Pageable. - Updating a patient's name with a transactional modifying query.
- Fetching patients with their appointments and associated doctors.
The service layer coordinates entity relationships inside transactions:
AppointmentServicecreates appointments for a selected patient and doctor, and reassigns appointments to another doctor.InsuranceServiceassigns or removes a patient's insurance policy while maintaining both sides of the one-to-one association.PatientServicecontains an example of transactional entity retrieval and dirty checking.
The test suite contains Spring Boot integration tests for application startup, patient persistence, and insurance relationships. Ensure PostgreSQL is running and the datasource configuration is valid before executing the tests.
This repository currently provides the application core and does not include a web API. Typical next steps are:
- Add REST controllers and request/response DTOs.
- Add input validation and centralized exception handling.
- Replace schema recreation with versioned migrations such as Flyway or Liquibase.
- Move credentials to environment variables or a secrets manager.
- Add authentication, authorization, and API documentation.
No license has been specified for this repository. Add a license file before distributing or reusing the project outside its intended context.