0

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

@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);
}
}
Lova Chittumuri
  • 2,994
  • 1
  • 30
  • 33

2 Answers2

3

Your issue is related to a change included in Hibernate 5.4.12.Final which is included in Springboot 2.2.5.

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

To avoid this issue, you need to use the QueryHints. (javax.persistence.fetchgraph or javax.persistence.loadgraph)

1

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

@EntityGraph(attributePaths = {"image"})
Eklavya
  • 17,618
  • 4
  • 28
  • 57