@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String location;
@OneToMany(mappedBy = "department")
private List<Employee> employees;
// Getters and Setters
}
Abstract repo:
@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {
}
Service:
@Service
public class DepartmentService {
@Autowired
private DepartmentRepository departmentRepository;
public List<Department> getAllDepartments() {
return departmentRepository.findAll();
}
public Department getDepartmentById(Long id) {
return departmentRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Department not found"));
}
public Department saveDepartment(Department department) {
return departmentRepository.save(department);
}
}
It could look something like this:
Abstract repo:
Service: