java JPA存储库找不到与当前实体相关的现有实体

pcww981p  于 2023-01-04  发布在  Java
关注(0)|答案(1)|浏览(162)

我有一个代码,找到当前登录的用户,并找到它的用户名。

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,而不是用户,但仍然不能理解为什么不返回的记录,因为这个工作的另一个实体和回报。

ktecyv1j

ktecyv1j1#

这个问题还不清楚。我们没有看到你的仓库和实体类。你在哪个仓库中使用哪个实体。请与我们分享。我猜,用户实体有UserProfile实体,所以可以试试;

Optional<User> findByUserProfile(UserProfile userProfile);

因为用户应该具有用户配置文件。您在用户实体中具有@OneToOne用户配置文件关系

相关问题