14

Given this 3 entities:

@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 Department that returns all departments with their employees and nationalities. What I have achieved is to return all departments with their 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> {
}
Songo
  • 5,618
  • 8
  • 58
  • 96

1 Answers1

24

A way to do this is to create a Projection for your nested(s) object, and then use this projection in a more global one. So following your problem, you can create a projection for Nationality, then another one for Department that has a getter to catch Nationality's projection, and finally another projection to get Department's entity.

@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 it helps!

alonso_50
  • 1,442
  • 13
  • 16
  • Can you show how you would implement a global projection which will return the projections defined above? – Charlie Apr 26 '17 at 06:47
  • 1
    @DeltaCharlie actually it is right now in the code I showed. For example, please check that `EmployeeProjection` has nested the `NationalityProjection`, so `EmployeeProjection` is more global than `EmployeeProjection`. Same story happens for `DepartmentProjection` interface. – alonso_50 May 17 '17 at 10:28