Search code examples
spring-boothibernatejpaeager-loadingentitygraph

Why does @EntityGraph not load EAGER scince Spring version 2.2.5?


Before Spring Boot 2.2.5 @EntityGraph used to load EAGER, yet after 2.2.5 I need to add EAGER to the attributePaths, for example attributePaths = {"image", "roles"}

How @EntityGraph works or am i doing something wrong. The issue came up as I changed to the newer version 2.2.4 -> 2.2.5

Employee class:

@Entity
@Getter
@Setter
public class Employee {

    @Column
    private String email;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "employees_roles",
            joinColumns = @JoinColumn(name = "employees_id", nullable = false, referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "roles_id", nullable = false, referencedColumnName = "id")
    )
    private Set<Role> roles;


    @JoinColumn(name = "image_id")
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    private Image image;
}

EmployeeRepository class:

@Repository
interface EmployeeRepository extends JpaRepository<Employee, Long> {

    @EntityGraph(attributePaths = "image")
    Optional<Employee> findByEmailIgnoreCase(String email);
}

EmployeeController class:

@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/employee", produces = MediaType.APPLICATION_JSON_VALUE)
public class EmployeeController {

    private final EmployeeService employeeService;

    @GetMapping(value = "/login")
    public ResponseEntity<String> login(Principal user) throws IOException {
        Employee employee = employeeService.findByEmailIgnoreCase(user.getName())
                .orElseThrow(() -> new UsernameNotFoundException(USER_NOT_FOUND));

        return ResponseEntity.ok(employee);
    }
}

Solution

  • attributePaths of @EntityGraph takes String[] you are using String Try this way

    @EntityGraph(attributePaths = {"image"})