clinical_app
Prompt#1 :
Create a database called clinicals and use it.
Create tables with normalized design to properly capture patient and his clinical data and join them using patient key .
Insert sample data for 5 patients with 2 sample data each.
show databases;
drop database clinicals;
SHOW TABLES;
-- Create the database
CREATE DATABASE clinicals;
-- Use the database
USE clinicals;
-- Create Patients table
CREATE TABLE patient (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
age INT,
PRIMARY KEY (id)
);
-- Create Clinical Data table (joined to patient)
CREATE TABLE clinical_data (
id INT NOT NULL AUTO_INCREMENT,
patient_id INT NOT NULL,
component_name VARCHAR(255) NOT NULL,
component_value VARCHAR(255) NOT NULL,
measured_date_time TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (patient_id) REFERENCES patient(id)
);
-- Insert sample patients
INSERT INTO patient (first_name, last_name, age) VALUES
('John', 'Doe', 52),
('Jane', 'Smith', 32),
('Alice', 'Johnson', 22),
('Bob', 'Williams', 33),
('Emily', 'Brown', 55);
-- Insert sample clinical data (2 records per patient)
INSERT INTO clinical_data (patient_id, component_name, component_value, measured_date_time) VALUES
(1, 'bp', '120/80', '2025-08-01 10:00:00'),
(1, 'heartrate', '72', '2025-08-01 10:05:00'),
(2, 'bp', '130/85', '2025-08-02 11:00:00'),
(2, 'heartrate', '75', '2025-08-02 11:05:00'),
(3, 'bp', '110/70', '2025-08-03 12:00:00'),
(3, 'heartrate', '68', '2025-08-03 12:05:00'),
(4, 'bp', '125/82', '2025-08-04 13:00:00'),
(4, 'heartrate', '80', '2025-08-04 13:05:00'),
(5, 'bp', '118/78', '2025-08-05 14:00:00'),
(5, 'heartrate', '70', '2025-08-05 14:05:00');
Check:
select first_name, last_name, age, component_name, component_value, measured_date_time from patient p , clinical_data d where p.id = d.patient_id;
drop table patient;
drop table clinical_data;
drop database clinic;
__________________________________________________________________________________________________________________________________
prompt#2: Open the command pallete
@vscode create java project
VCC > Java project > Spring boot > maven project >
spring web
spring jpa
mysql driver
package name : com.clinic
rest_app
create a folders
1. model
2. repos
3. Dto
4. Controllers
5. resources
__________________________________________________________________________________________________________________________________
JPA can be defined as Java Persistence API
Creating the JPA Models :
Prompt#3:
Create a JPA model class for the below table
TABLE patient (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
age INT,
PRIMARY KEY (id)
);
Check for the below:
1) Packages - import jakarta.persistence.*;
2) Annotations - @Entity, @Table @OneToMany, @JoinColumn
@OneToMany(mappedBy = "patient", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
3) getters and setters for every datatypes and objects
import jakarta.persistence.*;
@Entity
@Table(name = "patient")
public class Patient {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Prompt#4:
Create a JPA model class for the below table with ManyToOne and JsonIgnore annotations
TABLE clinical_data (
id INT NOT NULL AUTO_INCREMENT,
patient_id INT NOT NULL,
component_name VARCHAR(255) NOT NULL,
component_value VARCHAR(255) NOT NULL,
measured_date_time TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (patient_id) REFERENCES patient(id)
);
Check for the below:
1) Packages - import jakarta.persistence.*;
2) Annotations - @Entity, @Table, @ManyToOne, @JoinColumn, @JsonIgnore, @CreationTimestamp
3) getters and setters for every datatypes and objects
@Entity
@Table(name = "clinical_data")
public class ClinicalData {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JoinColumn(name = "patient_id", nullable = false)
change the package to import java.sql.Timestamp;
Check the ManyToOne and OneToMany dependancies in JPA.
_____________________________________________________________________________________________
Create the JPA Model Repositories:
Create the JPA Model Repository for the JPA Model Patient.java in repos folder
Create the JPA Model Repository for the JPA Model ClinicalData.java in repos folder
Check:
1. import the model java
2. @Repository
3. interface ClinicalDataRepository extends JpaRepository
_____________________________________________________________________________________________
Create Dto:
Create a PatientDataRequest java file
Create a ClinicalDataRequest java file
Checks:
1. patient id should be integer
2. getters and setters
_____________________________________________________________________________________________
Rest Controllers
Prompt:
Create a Rest controller that uses PatienetRepository.java and exposes a rest api to perform CRUD operations
Checks:
import section
model class
repo class
Annotations:
@RestController
@RequestMapping("/api/patients")
@CrossOrigin(origins = "*")
CRUD operations
Prompt:
Create a Rest controller that uses ClinicalDataRepository.java and exposes a rest api to perform CRUD operations
import section
model class
Annotations:
@RestController
@RequestMapping("/api/patients")
@CrossOrigin(origins = "*")
@annotate repo classes
CRUD operations
Add a save method uses ClinicalDataRequest object as parameter to save clinical data
_____________________________________________________________________________________________
Configure Data Source and properties
spring.application.name=clinicals_rest_app
spring.datasource.url=jdbc:mysql://localhost:3306/clinic
spring.datasource.username=root
spring.datasource.password=MysqlAdmin@123
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
server.servlet.context-path=/clinicalservices
_____________________________________________________________________________________________
Main SpringBootApplication:
@SpringBootApplication
public class ClinicalsDemoApplication {
Run the stand alone application using Postman.
Pre - Checks:
Package structure - main class package is base . controller , model, repos packages are subpackages
Run the
ClinicalsDemoApplication.java
Logs check:
Tomcat started on port 8080 (http) with context path '/clinicalservices'
port is ok
context path is ok
Test using postman or browser:
http://localhost:8080/clinicalservices/api/patients
http://localhost:8080/clinicalservices/api/patients/4
http://localhost:8080/clinicalservices/api/clinicaldata
_____________________________________________________________________________________________
Possible errors:
Helper : SQL Error: 1045, SQLState: 28000
2025-08-21T09:21:38.550+08:00 ERROR 2528 --- [clinicals_rest_app] [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : Access denied for user 'root'@'localhost' (using password: YES)
2025-08-21T09:21:38.552+08:00 WARN 2528 --- [clinicals_rest_app] [ main] o.h.e.j.e.i.JdbcEnvironmentInitiator : HHH000342: Could not obtain connection to query metadata
org.hibernate.exception.GenericJDBCException: unable to obtain isolated JDBC connection [Access denied for user 'root'@'localhost' (using password: YES)] [n/a]
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:63) ~[hibernate-core-6.6.22.Final.jar:6.6.22.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:108) ~[hibernate-core-6.6.22.Final.jar:6.6.22.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:94) ~[hibernate-core-6.6.22.Fi
_____________________________________________________________________________________________
Create a React project
1. npx create-react-app@latest clinicals-webapp
2. npm install react-router-dom
3. npm install axios
4. npm install react-toastify
cd clinicals-app
npm start
Open the clinicals-app in Code editor
create a component folder
Create a place holder files
Home.js
Patient.js
Clinical.js
export default function Home (){
return (
<b> Home </b>
);
}
App.js
1. Create a Header with light background and minimum height 10vh
2. Create a <NAV> and nav list with background , flex type , list style none , horizontal menu type, left sided
3. Add Routing details
<BrowseRouter>
<Routes>
<Route>
<Route>
<Route>
</Routes>
</BrowseRouter>
4. Create a Footer
5. Add ToastContainer
_________________________________________________________________________________________________________________________________
Impement Home.js
Prompt : react component that makes a api call using axios to fetch the patient details and then renders them as html table.
check the Name Home
Check the constants patients and set patients
fetch() - set the path axios.get ('http://localhost:8080/patientservices/patients')
useeffect as soon as mounted ( [] )
rendering - return statement
CORS issue - (No access is allowed for cross origint request )
__________________________________________________________________________________________________________________________________
CORS Error fix in Spring boot
Add the annotation
@CrossOrigin(origins="*")
__________________________________________________________________________________________________________________________________
Add Patient.js
prompt : Create a React component that gathers patinet information like first name, last name , age and makes axios api call to save the patient object.
check the name
check the constatnts for each field
check the functions to set the values
check the handle submit function
check the axios api call
__________________________________________________________________________________________________________________________________
React Toastify
<ToastContainer autoClose={2000}/>
import { toast} from 'react-toastify'
__________________________________________________________________________________________________________________________________
Add Clinical Data - Part 1
Prompt: Retrieve the patient details for a given Patient Id and render the details in table.
Check the name
Check the constatnts - parent
Check the param -
Add Clinical Data - Part 2
Prompt: Gather the component name , value using html forms and make an api call using axios to save the data.
submit function - axios fetch
__________________________________________________________________________________________________________________________________
Add Back links
__________________________________________________________________________________________________________________________________
Styling
___________________________________________________________________________________
Datasource:
Possible errors:
Helper : SQL Error: 1045, SQLState: 28000
2025-08-21T09:21:38.550+08:00 ERROR 2528 --- [clinicals_rest_app] [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : Access denied for user 'root'@'localhost' (using password: YES)
2025-08-21T09:21:38.552+08:00 WARN 2528 --- [clinicals_rest_app] [ main] o.h.e.j.e.i.JdbcEnvironmentInitiator : HHH000342: Could not obtain connection to query metadata
org.hibernate.exception.GenericJDBCException: unable to obtain isolated JDBC connection [Access denied for user 'root'@'localhost' (using password: YES)] [n/a]
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:63) ~[hibernate-core-6.6.22.Final.jar:6.6.22.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:108) ~[hibernate-core-6.6.22.Final.jar:6.6.22.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:94) ~[hibernate-core-6.6.22.Fi
Fix:
spring.datasource.url=jdbc:mysql://localhost:3306/clinic - clinic is a database name
spring.datasource.username=root
spring.datasource.password=MysqlAdmin@123 - correct password
_____________________________________________________________________________________________
Controller is not working:
Check the application logs for the full stack trace of the error. It will show the exact cause.
Make sure all clinical_data rows in your database have valid patient_id values that exist in the patient table.
Ensure your ClinicalData entity uses the correct type for the measuredDateTime field (e.g., Timestamp or LocalDateTime) and matches the database column type.
If you have circular references (e.g., Patient has a list of ClinicalData and vice versa), use @JsonIgnore or @JsonManagedReference/@JsonBackReference to avoid infinite recursion in JSON serialization.
_____________________________________________________________________________________________
This error is caused by Jackson (JSON serialization) trying to serialize a Hibernate proxy object, usually due to lazy loading and bidirectional relationships.
How to fix:
Add @JsonIgnore to the patient field in ClinicalData:
@ManyToOne
@JoinColumn(name = "patient_id", nullable = false)
@JsonIgnore
private Patient patient;
Or, use @JsonManagedReference and @JsonBackReference for bidirectional relationships:
On the patient field in ClinicalData:
@ManyToOne
@JoinColumn(name = "patient_id", nullable = false)
@JsonBackReference
private Patient patient;
On the list of clinical data in Patient (if present):
@OneToMany(mappedBy = "patient")
@JsonManagedReference
private List<ClinicalData> clinicalDataList;
___________________________________________________________________________________
Comments
Post a Comment