Spring 数据并不急切

Spring data doesn't get eager

我正在迁移我的代码以使用存储库,当我尝试使用 findOne(Integer) 获取 User 时,我得到了用户,但 userRoles 是空的。

存储库:

@RepositoryDefinition(domainClass = User.class, idClass = Integer.class)
public interface UserRepository {
   User findOne(Integer id);
   ...
}

用户:

public class User implements UserDetails{
   ...
   @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
   @JoinColumn(name = "userId")
   private Set<UserRole> userRoles = new HashSet<>(0);
   ...
}

用户角色:

public class UserRole implements GrantedAuthority {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private Integer userRoleId;
    @Column(length = 45, nullable = false)
    @Enumerated(EnumType.STRING)
    private Role role;
    ...
}

我用 join 定义了我自己的查询来解决它:

@RepositoryDefinition(domainClass = User.class, idClass = Integer.class)
public interface UserRepository {
    @Query("from User u join u.userRoles where u.id = ?1")
    User findOne(Integer id);
       ...
}