javax.persistence.RollbackException: Error while committing the transaction] with root cause java.lang.StackOverflowError: null

江枫思渺然 提交于 2019-12-05 22:08:05

The issue was to do with the way I implemented the AuditorAware< T > interface. The userDao method I was using was causing a recursive call. I still don't know why it was happening, but looking at this forum, I changed the implementation of getCurrentAuditor() from:

@Override
public Optional<User> getCurrentAuditor() {
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userDao.findByUsername(username);
    return Optional.ofNullable(user);
}

to:

@Override
public Optional<User> getCurrentAuditor() {
    User auditor = null;
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof User) {
            auditor = (User) principal;
        }
    }
    return Optional.ofNullable(auditor);
}

and everything works as expected.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!