2016-08-11 66 views
5

鑑於這種3個實體:如何在Spring數據庫中返回深度嵌套投影?

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

@Entity 
class Employee{ 
    Nationality nationality; 
    Nationality getNationality(){ 
     this.nationality; 
    } 
} 
@Entity 
class Nationality{ 

} 

我想創建Department投影與他們的員工和民族返回所有部門。我已經實現是使用自己的員工返回所有部門:

@Projection(name = "fullDepartment", types = { Department.class }) 
public interface DepartmentsProjection { 
    Set<Employee> getEmployees(); 
} 

@RepositoryRestResource(collectionResourceRel = "department", path = "departments") 
public interface DepartmentRepository extends JpaRepository<Department, Long> { 
} 
+1

您是否設法解決此問題? – thorinkor

回答

5

做到這一點的方法是爲您的嵌套(一個或多個)對象中的投影,然後在一個更加全球化的一個使用此投影。因此,在你的問題出現之後,你可以爲國籍創建一個投影,然後爲部門創建一個投影,以獲取國籍的投影,最後再獲得部門實體的投影。

@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(); 
} 

希望它有幫助!

+0

你能告訴你如何實現一個全局投影,它將返回上面定義的投影? –

+0

@DeltaCharlie實際上現在在我展示的代碼中。例如,請檢查「EmployeeProjection」是否嵌套了「NationalityProjection」,因此「EmployeeProjection」比「EmployeeProjection」更具全局性。 'DepartmentProjection'界面也會出現同樣的情況。 –