为什么@EntityGraph没有加载EAGER scince Spring 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);
}
}

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

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

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

@EntityGraphattributePaths取您正在使用的StringString[]尝试这种方式

@EntityGraph(attributePaths = {"image"})

最新更新