gpt4 book ai didi

java - MongoDB 和 Spring : How to correctly query with two fields in one repository query?

转载 作者:行者123 更新时间:2023-12-02 08:39:47 25 4
gpt4 key购买 nike

我有以下POJO:

    public class Round {

private ObjectId _id;

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

// rest of fields

}

在我的 Spring boot 项目中,我尝试通过 userId 和 _id 查询我的 mongoDB,如下所示:

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

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

}

但是,当我尝试 gradlew bootRun 时,我现在得到:

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.

使用 2 个参数查询 MongoDB正确方法是什么?

最佳答案

您必须在 Round 类中使用注释@Document,并在 ID 属性中使用注释@Id:

@Document
public class Round {


@Id
private ObjectId _id;

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

// rest of fields

}

关于java - MongoDB 和 Spring : How to correctly query with two fields in one repository query?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61446650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com