Data Repositories
Relevant source files
The following files were used as context for generating this wiki page:
- Part1/API/src/main/java/com/example/api/repository/AssignmentRepository.java
- Part1/API/src/main/java/com/example/api/repository/DepartmentRepository.java
- Part1/API/src/main/java/com/example/api/repository/EmployeeRepository.java
- Part1/API/src/main/java/com/example/api/repository/UserRepository.java
- Part1/API/src/main/java/com/example/api/model/Employee.java
- Part1/API/src/main/java/com/example/api/model/Department.java
- Part1/API/src/main/java/com/example/api/model/Assignment.java
Data Repositories in the SafeDispatch HQ Administration API serve as the abstraction layer between the domain models and the underlying database. Built on Spring Data JPA, these interfaces facilitate Create, Read, Update, and Delete (CRUD) operations and provide custom query capabilities to handle complex relationships between employees, departments, and their assignments.
The repository layer is central to the application's architecture, enabling services and controllers to persist and retrieve data without managing boilerplate JPA or SQL code. It handles core entities such as Employee, Department, Assignment, and security-related AppUser data.
Repository Architecture
The project utilizes the JpaRepository interface from Spring Data JPA to provide standard data access methods. This architecture allows for the definition of custom query methods using the @Query annotation when standard method naming conventions are insufficient for complex joins.
Entity Relationships
The data layer manages a relational schema where employees are linked to departments through an intermediary Assignment entity.
The diagram illustrates the many-to-many relationship between Employees and Departments facilitated by the Assignment entity.
Sources: Employee.java:1-55, Department.java:1-45, Assignment.java:1-40
Core Repositories
EmployeeRepository
Responsible for managing Employee persistence. Beyond standard CRUD operations, it includes specialized queries to filter employees based on their departmental assignments.
- Custom Queries: Uses a JPQL query to join with the
Assignmenttable to find employees belonging to a specific department by name. - Key Methods:
findByDepartmentName,findByEmail.
Sources: EmployeeRepository.java:12-28
DepartmentRepository
Manages Department entities and provides access to the collection of employees associated with a department.
- Custom Queries: Retrieves a list of
Employeeobjects by joining through theAssignmententity using a department ID. - Key Methods:
findEmployeesByDepartmentId.
Sources: DepartmentRepository.java:13-20
AssignmentRepository
A standard repository for the Assignment entity. It manages the links between employees and departments, including metadata such as the specific role and access level held within that assignment.
Sources: AssignmentRepository.java:1-12
UserRepository
Specifically used for security and authentication. It manages AppUser entities, which are distinct from business Employee entities and are used by Spring Security for role-based access control.
Sources: UserRepository.java:1-13, AppUser.java:1-35
Data Model Specifications
The following table summarizes the fields and constraints defined within the data models handled by the repositories:
| Model | Field | Type | Constraints | Description |
|---|---|---|---|---|
| Employee | String | Unique identifier for contact | ||
| Employee | contractType | String | @NotNull | Employment status (e.g., FULL_TIME) |
| Department | budget | Integer | @Positive | Financial allocation |
| Assignment | employee | Employee | @ManyToOne, non-null | Reference to assigned staff |
| Assignment | department | Department | @ManyToOne, non-null | Reference to assigned unit |
| AppUser | username | String | Unique, non-null | Security credential |
Sources: Employee.java:23-45, Department.java:18-25, Assignment.java:11-20, AppUser.java:13-20
Implementation Example: Custom JPQL
The repositories use the @Query annotation to perform complex fetches across the mapping table. For example, retrieving employees for a department requires navigating the Assignment relationship.
@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {
// Get all employees in a department by id
@Query("SELECT a.employee FROM Assignment a WHERE a.department.id = :deptId")
List<Employee> findEmployeesByDepartmentId(@Param("deptId") Long deptId);
}
Sources: DepartmentRepository.java:13-20
Summary
The Data Repository layer provides a robust and type-safe mechanism for data persistence in the SafeDispatch API. By leveraging Spring Data JPA and custom JPQL queries, the system efficiently manages the lifecycle of employees and departments while maintaining the integrity of the assignments that link them. This layer ensures that business logic remains decoupled from database-specific implementation details.