1

I have the following POJO:

    public class Round {

        private ObjectId _id;

        @NotEmpty
        @Getter
        @Setter
        @Accessors(fluent = true)
        @JsonProperty("userId")
        private String userId;

        // rest of fields

}

In my Spring boot project I am trying to query my mongoDB by userId and _id as follows:

@Repository
public interface RoundRepository extends MongoRepository<Round, String> {

    Optional<Round> findByUserIdAnd_id(String userId, ObjectId objectId);

}

However when I try to gradlew bootRun I now get:

Unsatisfied dependency expressed through constructor parameter 0; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'roundRepository': Invocation of init method failed; nested exception is 
org.springframework.data.mapping.PropertyReferenceException: No property and found for type String! Traversed path: Round.userId.

What is the correct way to query MongoDB with 2 parameters?

java12399900
  • 1,485
  • 7
  • 26
  • 56
  • I wonder if the underscore is causing trouble as described in https://stackoverflow.com/questions/23456197/spring-data-jpa-repository-underscore-on-entity-column-name. Can you verify a query for two parameters works if they are named without underscore? – Michael Kreutz Apr 26 '20 at 19:23
  • There's also an error with the creation of bean 'roundRepository'. –  Apr 26 '20 at 20:17
  • try changing the name of the variable "_id" –  Apr 26 '20 at 20:18

1 Answers1

2

You have to use annotation @Document in Round class and the annotation @Id for ID attribute:

@Document
public class Round {


        @Id
        private ObjectId _id;

        @NotEmpty
        @Getter
        @Setter
        @Accessors(fluent = true)
        @JsonProperty("userId")
        private String userId;

        // rest of fields

}