Advertisement
Google Ad Slot: content-top
Spring Boot One-to-One Mapping
A One-to-One relationship means one entity is associated with exactly one other entity.
Example:
✅ A User has exactly one Profile
✅ A Profile belongs to exactly one User
Define User and Profile Entities
We'll create a bidirectional relationship where both User and Profile reference each other.
✅ @OneToOne(cascade = CascadeType.ALL) → Ensures changes in User also apply to Profile.
✅ @JoinColumn(name = "profile_id") → Defines the foreign key column in the User table.
Table name : profile
| id | bio | phone |
|---|
Table name : user
| id | name | profile_id |
|---|
Create JPA Repositories
Create Services
Create Controllers
Create REST API Controller for Profile
Testing the REST API with Postman:
Once the application is running, test the endpoints:
Create Profile http://localhost:8080/profiles
Get All Profiles http://localhost:8080/profiles
Get Profile By Id http://localhost:8080/profiles/{profileID}
Update Profile http://localhost:8080/profiles
Delete Profile http://localhost:8080/profiles/{profileID}
Create REST API Controller for User
Testing the REST API with Postman:
Once the application is running, test the endpoints:
Create User With Profile http://localhost:8080/users
Get All Users With Profile http://localhost:8080/users
Get Users With Profile by user id http://localhost:8080/users/{userID}
Update Users http://localhost:8080/users
Delete Users http://localhost:8080/users/{userID}
Create Users with existing profile id http://localhost:8080/users/existingProfile/{profileID}
Update Existing Users With Existing Profile http://localhost:8080/users/existingUserProfile/{profileID}/{userID}
Update Profile by Using User id http://localhost:8080/users/{userID}/profile/update-name?newProfileName=Mobile Developer
Delete Profile by Using User id http://localhost:8080/users/{userID}/profile