我有一个代码,找到当前登录的用户,并找到它的用户名。
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
MyUserDetails principal = (MyUserDetails) authentication.getPrincipal();
current = userService.findByUsername(principal.getUsername());
在此之后,我尝试查找与从UserProfileService
返回的当前登录用户相关的实体
userProfile = userProfileService.findByUser(current);
这对于其他实体来说是正常的,但是对于userProfile
,实体不会返回与以下实体之间存在userProfile
关系的记录
用户配置文件实体字段Map
@OneToOne(mappedBy = "userProfile")
private User user;
userProfile的用户实体字段引用
@OneToOne
@JoinColumn(name = "user_profile_id", referencedColumnName = "id")
private UserPrifile userProfile;
用户配置文件存储库方法
Optional<UserProfile> findByUser(User user);
这不起作用,它没有在数据库中找到与当前用户关系的用户配置文件。是为其他实体工作,但不是为这个任何线索,哪里会有问题
编辑:我的工作围绕它的搜索用户ID,而不是用户,但仍然不能理解为什么不返回的记录,因为这个工作的另一个实体和回报。
1条答案
按热度按时间ktecyv1j1#
这个问题还不清楚。我们没有看到你的仓库和实体类。你在哪个仓库中使用哪个实体。请与我们分享。我猜,用户实体有UserProfile实体,所以可以试试;
因为用户应该具有用户配置文件。您在用户实体中具有@OneToOne用户配置文件关系