Spring Practice Questions 1
Spring Practice Questions 1
Create a Spring Boot application with two entities: "Teacher" and "TeacherProfile." Each
Teacher can have one TeacherProfile, and each TeacherProfile can be assigned to one
Teacher. Implement a bidirectional one-to-one mapping between these entities using Spring
Data JPA.
Functional Requirements:
Create folders named controller, model, repository, and service inside the
WORKSPACE/springapp/src/main/java/com/example/springapp.
Inside the model folder, create a class named Teacher with the following attributes:
• name - String
• subject - String
• email - String
Implement getters, setters, and constructors (both parameterized and no-argument) for the
Teacher and TeacherProfile entities.
Inside the repository folder, create interfaces named TeacherRepo and TeacherProfileRepo.
Inside the service folder, create interfaces named TeacherService and TeacherProfileService.
Project Structure:
WORKSPACE/springapp/src/main/java/com/example/springapp
│
├── controller
│ ├── TeacherController.java
│ └── TeacherProfileController.java
├── model
│ ├── Teacher.java
│ └── TeacherProfile.java
├── repository
│ ├── TeacherRepo.java
│ └── TeacherProfileRepo.java
│
└── service
├── TeacherService.java
├── TeacherProfileService.java
├── TeacherServiceImpl.java
└── TeacherProfileServiceImpl.java
API ENDPOINTS:
• POST - /api/teacher - Returns response status 201 with teacher object on successful
creation or else 500. In case of a DuplicateTeacherException, specifically when the
email already exists, it returns a status of 409 (CONFLICT) with an appropriate error
message as "Teacher with email {email} already exists!".
QUESTION 2:
Overview:
Create a Spring Boot application with two entities: Manager and Department. Each
Manager can be associated with one Department, and each Department can have one
Manager. Implement a unidirectional one-to-one mapping from Manager to Department using
Spring Data JPA.
Functional Requirements:
Manager Attributes:
• name - String
• email - String
• department - Department (@OneToOne, @JoinColumn, @JsonBackReference)
Department Attributes:
• name - String
• location - String
Implement getters, setters, and constructors (both parameterized and no-argument) for the
Manager and Department entities.
css
Copy code
WORKSPACE/springapp/src/main/java/com/example/springapp
│
├── controller
│ ├── ManagerController.java
│ └── DepartmentController.java
├── model
│ ├── Manager.java
│ └── Department.java
│
├── repository
│ ├── ManagerRepository.java
│ └── DepartmentRepository.java
└── service
├── ManagerService.java
├── DepartmentService.java
├── ManagerServiceImpl.java
└── DepartmentServiceImpl.java
API Endpoints:
• POST - /api/departments - Returns status 201 with the created Manager object on
success. In case of a conflict, returns 409 (CONFLICT) with an appropriate error
message.
• GET - /api/managers - Returns status 200 with a list of all Manager objects or 404 if
no managers are found.
QUESTION 3:
Overview:
Create a Spring Boot application with two entities: Employee and ProfileDetails. Each
Employee can have one ProfileDetails, and each ProfileDetails is associated with one
Employee. Implement a unidirectional one-to-one mapping from ProfileDetails to Employee
using Spring Data JPA.
Functional Requirements:
Inside the model folder, create a class named Employee with the following attributes:
• department - String
• address - String
• phoneNumber - String
• dateOfBirth - LocalDate
Implement getters, setters, and constructors (both parameterized and no-argument) for the
Employee and ProfileDetails entities.
Inside the repository folder, create interfaces named EmployeeRepo and ProfileDetailsRepo.
Inside the service folder, create interfaces named EmployeeService and ProfileDetailsService.
Also, create classes EmployeeServiceImpl and ProfileDetailsServiceImpl that implement
EmployeeService and ProfileDetailsService, respectively.
Project Structure:
css
Copy code
WORKSPACE/springapp/src/main/java/com/example/springapp
│
├── controller
│ ├── EmployeeController.java
│ └── ProfileDetailsController.java
├── model
│ ├── Employee.java
│ └── ProfileDetails.java
│
├── repository
│ ├── EmployeeRepo.java
│ └── ProfileDetailsRepo.java
└── service
├── EmployeeService.java
├── ProfileDetailsService.java
├── EmployeeServiceImpl.java
└── ProfileDetailsServiceImpl.java
API Endpoints:
• POST - /api/employees - Creates a new Employee object and returns response status
201 upon successful creation. In case of a conflict, returns status 409 (CONFLICT)
with an error message.
• POST - /api/profile-details/employees/{employeeId} - Creates a new ProfileDetails
object and returns response status 201 upon successful creation or 500 in case of an
error.
QUESTION 4:
Overview:
Create a Spring Boot application with two entities: Library and LibraryDetails. Each
Library can have one LibraryDetails, and each LibraryDetails is associated with one Library.
Implement a unidirectional one-to-one mapping from LibraryDetails to Library using Spring
Data JPA.
Functional Requirements:
Create folders named controller, model, repository, and service inside
WORKSPACE/springapp/src/main/java/com/example/springapp.
Inside the controller folder, create classes named LibraryController and
LibraryDetailsController.
Inside the model folder, create a class named Library with the following attributes:
• libraryId - Long (auto-generated primary key)
• libraryName - String
• location - String
• contactInfo - String
• establishedYear - int
• numberOfBooks - int
• openingHours - String
• websiteUrl - String
Implement getters, setters, and constructors (both parameterized and no-argument) for the
Library and LibraryDetails entities.
Inside the repository folder, create interfaces named LibraryRepo and LibraryDetailsRepo.
Inside the service folder, create interfaces named LibraryService and LibraryDetailsService.
Also, create classes LibraryServiceImpl and LibraryDetailsServiceImpl which implement
LibraryService and LibraryDetailsService, respectively.
Project Structure:
css
Copy code
WORKSPACE/springapp/src/main/java/com/example/springapp
│
├── controller
│ ├── LibraryController.java
│ └── LibraryDetailsController.java
├── model
│ ├── Library.java
│ └── LibraryDetails.java
├── repository
│ ├── LibraryRepo.java
│ └── LibraryDetailsRepo.java
└── service
├── LibraryService.java
├── LibraryDetailsService.java
├── LibraryServiceImpl.java
└── LibraryDetailsServiceImpl.java
API Endpoints:
• POST - /api/libraries - Returns a 201 response status with the created Library object
upon successful creation. If a Library with the same name already exists, throws a
DuplicateLibraryException with the message "Library with name {libraryName}
already exists!" or a 500 status in the event of an error.
• PUT - /api/libraries/{id} - Returns a 200 response status with the updated Library
object upon a successful update. If the library is not found, it returns a 404 status.