Saturday, March 9, 2024

Database - Spring Boot Entities example for a Hospital

In a hospital management system, you might have entities like Hospital, Doctor, and Patient. 

In Spring Boot, you can use JPA (Java Persistence API) for entity mapping. Below are simplified entity classes for Hospital, Doctor, and Patient relationships:

Hospital Entity:

A hospital can have many doctors and many patients.

import javax.persistence.*;

import java.util.HashSet;

import java.util.Set;


@Entity

public class Hospital {


    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;


    private String name;


    @OneToMany(mappedBy = "hospital")

    private Set<Doctor> doctors = new HashSet<>();


    @OneToMany(mappedBy = "hospital")

    private Set<Patient> patients = new HashSet<>();


    // getters and setters


    // other constructors, methods, etc.

}

Doctor Entity:

A doctor can be assigned to multiple patients and work at one hospital.


import javax.persistence.*;

import java.util.HashSet;

import java.util.Set;


@Entity

public class Doctor {


    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;


    private String name;


    @ManyToOne

    @JoinColumn(name = "hospital_id")

    private Hospital hospital;


    @OneToMany(mappedBy = "doctor")

    private Set<Patient> patients = new HashSet<>();


    // getters and setters


    // other constructors, methods, etc.

}

Patient Entity:

 A patient can have one assigned doctor and be associated with one hospital.

import javax.persistence.*;


@Entity

public class Patient {


    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;


    private String name;


    @ManyToOne

    @JoinColumn(name = "hospital_id")

    private Hospital hospital;


    @ManyToOne

    @JoinColumn(name = "doctor_id")

    private Doctor doctor;


    // getters and setters


    // other constructors, methods, etc.

}

These entities use JPA annotations for defining the relationships. Note that these are simplified examples, and you might need to tailor them based on your specific requirements and business logic. Additionally, you may want to add more fields, validations, and methods to these entities as needed.


In a Spring Boot application with JPA, you typically create repositories to interact with the database using Spring Data JPA. Below are examples of how you might create repositories for the `Hospital`, `Doctor`, and `Patient` entities:

Hospital Repository:

import org.springframework.data.jpa.repository.JpaRepository;


public interface HospitalRepository extends JpaRepository<Hospital, Long> {

    // You can add custom queries or methods if needed

}


Doctor Repository:

import org.springframework.data.jpa.repository.JpaRepository;


public interface DoctorRepository extends JpaRepository<Doctor, Long> {

    // You can add custom queries or methods if needed

}

Patient Repository:

import org.springframework.data.jpa.repository.JpaRepository;


public interface PatientRepository extends JpaRepository<Patient, Long> {

    // You can add custom queries or methods if needed

}

By extending `JpaRepository`, you get a set of predefined CRUD methods, and you can add custom queries or methods by defining them in the repository interface. Spring Data JPA automatically generates the necessary queries based on method names and parameters.

For example, you could define a method in `DoctorRepository` to find doctors by hospital:

import java.util.List;


public interface DoctorRepository extends JpaRepository<Doctor, Long> {

    List<Doctor> findByHospital(Hospital hospital);

}

These repositories can then be injected into your services or controllers to interact with the database. Spring Boot and Spring Data JPA will handle the underlying database operations based on the methods you define in the repositories.


Remember to configure your application to enable JPA and set up the database connection in your `application.properties` or `application.yml` file. Here is an example:


# Database configuration

spring.datasource.url=jdbc:mysql://localhost:3306/your_database

spring.datasource.username=your_username

spring.datasource.password=your_password

spring.jpa.hibernate.ddl-auto=update


Replace `your_database`, `your_username`, and `your_password` with your actual database details.

These are basic examples, and you might need to customize them based on your specific requirements.

No comments:

Post a Comment

LeetCode C++ Cheat Sheet June

🎯 Core Patterns & Representative Questions 1. Arrays & Hashing Two Sum – hash map → O(n) Contains Duplicate , Product of A...