spring-bootspring-data-jpaentitygraph

@NamedEntityGraphs on findAll()


I use @NamedEntityGraph in my application

@NamedEntityGraphs({
    @NamedEntityGraph(name = "graph.Employee.assignments", attributeNodes = @NamedAttributeNode("assignmentProjectEmployeeSet")),
    @NamedEntityGraph(name = "graph.Employee.absences", attributeNodes = @NamedAttributeNode("absences"))
})enter code here`public class Employee {

And when I use it on the repository via

@EntityGraph(value = "graph.Employee.assignments", type = EntityGraph.EntityGraphType.FETCH)
List<Employee> findAll();

I got all results as expected. But I would need several versions of findAll() for the different situations. Something like

@EntityGraph(value = "graph.Employee.assignments", type = EntityGraph.EntityGraphType.FETCH)
List<Employee> findAllWithAssignments();

@EntityGraph(value = "graph.Employee.absences", type = EntityGraph.EntityGraphType.FETCH)
List<Employee> findAllWithAbsences();

But if I try to define a new method name in the repository, I get an Application Context error, since Spring is not able to resolve the mehtod name.

Is there a possibility to get such methods?

Thanks

Matthias


Solution

  • Add @Query to your methods which will select all Employees:

    @Query("select e from Employee e")
    @EntityGraph(value = "graph.Employee.assignments", type = EntityGraph.EntityGraphType.FETCH)
    List<Employee> findAllWithAssignments();
    
    @Query("select e from Employee e")
    @EntityGraph(value = "graph.Employee.absences", type = EntityGraph.EntityGraphType.FETCH)
    List<Employee> findAllWithAbsences();
    

    Or using method names findAllWithAssignmentsBy findAllWithAbsencesBy should also work