org.hibernate.LazyInitializationException: не удалось инициализировать прокси [ltxrest.ltx.model.Role # 2] - нет сеанса? - PullRequest
0 голосов
/ 27 марта 2020

Я сталкиваюсь с LazyInitializationException, когда я получаю доступ к url ​​/ api / api / user / admin / panel / {email}, пока я сохраняю logging.level = trace, в противном случае мое приложение работает абсолютно так, как и предполагалось

это мое Роль класса сущности

@Table(name = "user_role",schema = "userdata")
@Data

public class Role {
    @Id
    private int id;
    private String role;
    @OneToMany(fetch = FetchType.LAZY,mappedBy = "role")
    private List<MyUser> user;
}

Контроллер MyAdmin

@RestController
@RequestMapping("/api/user/admin/panel")
public class AdminController {
    private final Logger logger = LoggerFactory.getLogger(AdminService.class);

    @Autowired
    private AdminService adminService;
    @GetMapping(path = "/{email}")
    public UserDto fetchUser(@PathVariable("email") String email, Authentication auth) throws UsernameNotFoundException {
        logger.info("Getting user with email:"+email+" for admin:"+auth.getName());
        return adminService.getUser(email);
    }
    @PutMapping(path = "/{email}/{role}")
    public String updateUserRole(@PathVariable("email")String email,@PathVariable("role") String role
            ,Authentication auth){
        logger.info("Updating user:"+email+" to role:"+role+" by ADMIN:"+auth.getName());
        adminService.updateUserRole(email,role);
        return "Successful";
    }

}

Я пробовал EAGER Fetch и он отлично работает, но EAGER Fetch - плохая стратегия

...