How to return deep nested projections to Spring data?

Given these 3 objects:

@Entity class Department{ Set<Employee> employees; Set<Employee> getEmployees(){ return this.employees; }; } @Entity class Employee{ Nationality nationality; Nationality getNationality(){ this.nationality; } } @Entity class Nationality{ } 

I want to create a projection for the Department , which returns all departments with its employees and nationalities. What I have achieved is to return all departments with my employees using:

 @Projection(name = "fullDepartment", types = { Department.class }) public interface DepartmentsProjection { Set<Employee> getEmployees(); } @RepositoryRestResource(collectionResourceRel = "department", path = "departments") public interface DepartmentRepository extends JpaRepository<Department, Long> { } 
+5
source share
1 answer

The way to do this is to create a projection for your nested object, and then use this projection in a more global one. So, following your problem, you can create a forecast for citizenship, and then another for the Department, which has a getter to track the projection of Citizenship, and finally another projection to get the department object.

 @Projection(name = "NationalityProjection", types = { Nationality.class }) public interface NationalityProjection{ // getters of all attributes you want to project } @Projection(name = "EmployeeProjection", types = { Employee.class }) public interface EmployeeProjection{ NationalityProjection getNationality(); } @Projection(name = "DepartmentProjection", types = { Department.class }) public interface DepartmentProjection{ Set<EmployeeProjection> getEmployees(); } 

Hope this helps!

+5
source

All Articles