为什么@EntityGraph 不加载 EAGER science Spring 版本 2.2.5?

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

在 Spring Boot 2.2.5 之前 @EntityGraph 用于加载 EAGER,但在 2.2.5 之后我需要将 EAGER 添加到 attributePaths,例如 attributePaths = {"image", "roles"}

@EntityGraph 是如何工作的,或者我做错了什么。当我更改为较新的版本 2.2.4 -> 2.2.5

时出现了这个问题
@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;
}

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

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

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

attributePaths of @EntityGraph 需要 String[] 你正在使用 String 试试这个方法

@EntityGraph(attributePaths = {"image"})

您的问题与 Hibernate 5.4 中包含的更改有关。12.Final Springboot 2.2.5 中包含的更改。

https://github.com/hibernate/hibernate-orm/pull/3164/files

要避免此问题,您需要使用 QueryHints。 (javax.persistence.fetchgraphjavax.persistence.loadgraph)