0

在 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);
}
}
4

2 回答 2

1

attributePaths您正在使用的@EntityGraph镜头 试试这种方式String[]String

@EntityGraph(attributePaths = {"image"})
于 2020-04-10T14:09:28.320 回答
1

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

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

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

于 2020-05-07T16:35:23.537 回答