Mongodb Mongo存储库错误:首先未找到Person类型的属性!,定义findByFirst_名称(字符串首_名称)时出错;

Mongodb Mongo存储库错误:首先未找到Person类型的属性!,定义findByFirst_名称(字符串首_名称)时出错;,mongodb,spring-boot,spring-mongodb,spring-mongo,mongorepository,Mongodb,Spring Boot,Spring Mongodb,Spring Mongo,Mongorepository,我有一个类person,它的属性是string类型的first\u name,现在我想在PersonRepository中定义一个find方法,它可以找到所有名字等于字符串的person。我像这样在Person存储库中添加此方法 List findByFirst\u name(String first\u name)但我得到一个错误,即person类中没有属性first。我已经定义了很多find方法,但是这些属性不包含。,所以我可以很容易地定义那些findBy方法。我只在名称字符串中包含(下划线

我有一个类person,它的属性是string类型的
first\u name
,现在我想在PersonRepository中定义一个find方法,它可以找到所有名字等于字符串的person。我像这样在Person存储库中添加此方法

List findByFirst\u name(String first\u name)
但我得到一个错误,即person类中没有属性
first
。我已经定义了很多find方法,但是这些属性不包含
,所以我可以很容易地定义那些findBy方法。我只在名称字符串中包含
(下划线)的属性时遇到这个问题。 由于某些原因,我无法将
first_name
更改为
firstname
,是否有任何错误?如何为具有
的属性创建findBy方法这是一种特殊情况吗

这是我的密码。 Person.java

@Document(collection="person")
public class Person {
    
    @Id
    private String id;
    private String first_name;
    private String last_name;
    private String gender;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getFirst_name() {
        return first_name;
    }
    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }
    public String getLast_name() {
        return last_name;
    }
    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
}
个人知识库

public interface PersonRepository extends MongoRepository<Person, String>{
    
    List<Person> findByGender(String gender);
    List<Person> findByFirst_name(String first_name); // throwing error (No property first found for type Person!)
    
}

公共接口PersonRepository扩展了MongoRepository{
列表findByGender(字符串性别);
List findByFirst_name(String first_name);//抛出错误(没有为Person类型找到第一个属性!)
}
错误: 原因:org.springframework.data.mapping.PropertyReferenceException:未首先找到类型Person的属性


解决此问题有两种方法:

  • 将实体字段设置为camelCase
  • 通过将下划线(如
    first\u name
    加倍)跳过
    first\u name中的下划线,使其看起来像
    findByFirst\u name(字符串first\u name)

  • 我尝试在findBy方法中使用双精度uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;