1) Design a Spring Boot program to create a CRUD (Create, Read, Update, Delete)
application using Hibernate for managing employee records. The program
should allow users to perform the following operations on the employee
database:
a) Add a new employee: The user can enter details like employee name,
department, and salary, and the program should add the employee to the
database.
b) Update employee details: The user can update the name, department, or
salary of an existing employee based on their employee ID.
c) Delete an employee: The user can delete an employee from the database
based on their employee ID.
d) Display all employees: The program should retrieve and display a list of
all employees and their details from the database.
e) Requirements:
i) Use Spring Boot to create the application and Hibernate to manage
the database.
ii) Implement JPA (Java Persistence API) for data access.
iii) Provide a RESTful API for performing CRUD operations on
employees.
iv) Implement exception handling to handle possible errors during
database interactions.
v) Cover Spring Boot and Hibernate topics, such as entity classes,
repositories, services, and controllers.
f) Note: Before running the program, make sure you have set up the
database and configured the connection in the [Link] file.
[Link]
package [Link].employee_management;
import [Link];
import [Link];
//import [Link];
@SpringBootApplication
public class EmployeeManagementApplication {
public static void main(String[] args) {
[Link]([Link], args);
}
}
[Link]
package [Link].employee_management.[Link];
import [Link].employee_management.[Link];
import [Link].employee_management.[Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping
public List<Employee> getAllEmployees() {
return [Link]();
}
@GetMapping("/{id}")
public ResponseEntity<Employee> getEmployeeById(@PathVariable Long id)
{
Employee employee = [Link](id)
.orElseThrow(() -> new RuntimeException("Employee not found
with id: " + id));
return [Link](employee);
}
@PostMapping
public Employee createEmployee(@RequestBody Employee employee) {
return [Link](employee);
}
@PostMapping("/batch")
public ResponseEntity<List<Employee>> createEmployees(@RequestBody
List<Employee> employees) {
List<Employee> savedEmployees =
[Link](employees);
return [Link](savedEmployees);
}
@PutMapping("/{id}")
public ResponseEntity<Employee> updateEmployee(@PathVariable Long id,
@RequestBody Employee employeeDetails) {
Employee employee = [Link](id,
employeeDetails);
return [Link](employee);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteEmployee(@PathVariable Long id) {
[Link](id);
return [Link]().build();
}
}
[Link]
package [Link].employee_management.[Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = [Link])
private Long id;
private String name;
private String department;
private Double salary;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
[Link] = id;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
[Link] = department;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
[Link] = salary;
}
}
[Link]
package [Link].employee_management.[Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler([Link])
public ResponseEntity<?> handleRuntimeException(RuntimeException ex,
WebRequest request) {
return new ResponseEntity<>([Link](), HttpStatus.NOT_FOUND);
}
@ExceptionHandler([Link])
public ResponseEntity<?> handleGlobalException(Exception ex, WebRequest
request) {
return new ResponseEntity<>([Link](),
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
[Link]
package [Link].employee_management.[Link];
import [Link].employee_management.[Link];
import [Link];
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
[Link]
package [Link].employee_management.[Link];
import [Link].employee_management.[Link];
import
[Link].employee_management.[Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
public List<Employee> getAllEmployees() {
return [Link]();
}
public Optional<Employee> getEmployeeById(Long id) {
return [Link](id);
}
public Employee saveEmployee(Employee employee) {
return [Link](employee);
}
public List<Employee> saveEmployees(List<Employee> employees) {
return [Link](employees);
}
public Employee updateEmployee(Long id, Employee employeeDetails) {
Employee employee = [Link](id)
.orElseThrow(() -> new RuntimeException("Employee not found
with id: " + id));
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
return [Link](employee);
}
public void deleteEmployee(Long id) {
Employee employee = [Link](id)
.orElseThrow(() -> new RuntimeException("Employee not found
with id: " + id));
[Link](employee);
}
}
Output: