4

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

1 Answers1

4

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

Denis Zavedeev
  • 7,627
  • 4
  • 32
  • 53
  • Hi caco3 thank you for your answer. The solution with @Query is working perfectly. Thank you for this. I also tried findAllWithAssignmentsBy() as a repository method, this was not working, since the repository expect an argument (because of the "by"). –  Jan 30 '19 at 06:11
  • @MatthiasLauber you're welcome. You tried to use method names ending with `By` both at the same time, right? – Denis Zavedeev Jan 30 '19 at 09:32
  • I am not sure about it any more, but I htink, I tried it for one methode. But i found out, that Query is not needed, if one use an argument. For examle I need a similary data base access where I search projects with respect to a given project leader. Here I was able to use the EntityGraph annotation and findAllWithProjectLeaderId( Long id) without Query –  Feb 04 '19 at 12:26