Search code examples
javaspring

Parameter does not have a name error when using mongo repository


I have next repository:

interface UserRepository extends MongoRepository<User, String> {

    Optional<User> findById(@Param("id") String id);

}

When I try to use method findById() I receive an error

Parameter org.springframework.data.mapping.Parameter@b84c8964 does not have a name

The only solution I found when searched is to add @Param annotation, but it didn't help me. What is the reason of that issue and how to fix that?

User entity:

@Document
class User implements UserDetails {

  @Id
  @NonNull
  private String id

  @NonNull
  @Indexed(unique = true)
  private String email

  @JsonIgnore
  @NonNull
  private String password

  @NonNull
  private String role

  @NonNull
  private UserInfo userInfo

  @Override
  Collection<? extends GrantedAuthority> getAuthorities() {
      return List.of(new SimpleGrantedAuthority(role))
  }

  @Override
  String getPassword() {
      return password
  }

  @Override
  String getUsername() {
      return email
  }

  @Override
  boolean isAccountNonExpired() {
      return true
  }

  @Override
  boolean isAccountNonLocked() {
      return true
  }

  @Override
  boolean isCredentialsNonExpired() {
      return true
  }

  @Override
  boolean isEnabled() {
      return true
  }

  void setEmail(@NonNull String email) {
      this.email = email
  }

  void setPassword(@NonNull String password) {
      this.password = password
  }

  void setRole(@NonNull String role) {
      this.role = role
  }

  @NonNull
  UserInfo getUserInfo() {
      return userInfo
  }

  void setUserInfo(@NonNull UserInfo userInfo) {
      this.userInfo = userInfo
  }

  @NonNull
  String getId() {
      return id
  }

  void setId(@NonNull String id) {
      this.id = id
  }

  @NonNull
  String getEmail() {
      return email
  }

  @NonNull
  String getRole() {
      return role
  }

  User(String email, String password, String role, UserInfo userInfo) {
    this.email = email
    this.password = password
    this.role = role
    this.userInfo = userInfo
}

}


Solution

  • I'm not sure, but I think the MongoRepository already has implemented that method. Can you try deleting it from your repository and use it directly on your service or controller?

    Also you need to set an empty constructor on your entity.

    But the parameter, have you checked your controller?