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?
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
}