Spring Data MongoDB在org.springframework.Data.mapping.PropertyPath中找不到类型的属性

Spring Data MongoDB在org.springframework.Data.mapping.PropertyPath中找不到类型的属性,mongodb,spring-data,mongodb-java,spring-data-mongodb,Mongodb,Spring Data,Mongodb Java,Spring Data Mongodb,我使用的是SpringDataMongoDB1.4.2.0版本。对于SpringDataMongoDB,我已经在一个位置创建了自定义存储库接口和实现,并创建了自定义查询函数getUsersName(Users) 然而,我仍然得到以下例外: Caused by: org.springframework.data.mapping.PropertyReferenceException: No property get found for type Users! at org.springframe

我使用的是SpringDataMongoDB1.4.2.0版本。对于SpringDataMongoDB,我已经在一个位置创建了自定义存储库接口和实现,并创建了自定义查询函数
getUsersName(Users)

然而,我仍然得到以下例外:

Caused by: org.springframework.data.mapping.PropertyReferenceException:
  No property get found for type Users! at org.springframework.data.mapping.PropertyPath.     (PropertyPath.java:75) at
    org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327) at 
    org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:359) at
    org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:359) at
    org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307) at
    org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270) at
    org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241) at
    org.springframework.data.repository.query.parser.Part.(Part.java:76) at
    org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:201) at
    org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:291) at 
    org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:271) at 
    org.springframework.data.repository.query.parser.PartTree.(PartTree.java:80) at 
    org.springframework.data.mongodb.repository.query.PartTreeMongoQuery.(PartTreeMongoQuery.java:47)
下面是我的Spring数据MongoDB结构:

      /*  Users Domain Object */

        @Document(collection = "users")
        public class Users {

        @Id
        private ObjectId id;

        @Field ("last_name")
        private String last_name;

        @Field ("first_name")
        private String first_name;

       public String getLast_name() {
          return last_name;
      }

      public void setLast_name(String last_name) {
        this.last_name = last_name;
     }

      public String getFirst_name() {
        return first_name;
     }

     public void setFirst_name(String first_name) {
        this.first_name = first_name;
     }
}

       /* UsersRepository.java main interface */

        @Repository
        public interface UsersRepository extends MongoRepository<Users,String>, UsersRepositoryCustom { 

             List findUsersById(String id);

         }

       /* UsersRepositoryCustom.java custom interface */

        @Repository
        public interface UsersRepositoryCustom {

           List<Users> getUsersName(Users users);
        }

       /* UsersRepositoryImpl.java custom interface implementation */

        @Component
        public class UsersRepositoryImpl implements UsersRepositoryCustom {

        @Autowired
        MongoOperations mongoOperations;


        @Override
        public List<Users> getUsersName(Users users) {
            return mongoOperations.find(
                    Query.query(Criteria.where("first_name").is(users.getFirst_name()).and("last_name").is(users.getLast_name())), Users.class);
        }

       /* Mongo Test function inside Spring JUnit Test class calling custom function with main UsersRepository interface */

        @Autowired
        private UsersRepository usersRepository;

        @Test
        public void getUsersName() {

            Users users = new Users();
            users.setFirst_name("James");`enter code here`
            users.setLast_name("Oliver");
            List<Users> usersDetails = usersRepository.getUsersName(users);
            System.out.println("users List" + usersDetails.size());


            Assert.assertTrue(usersDetails.size() > 0);
        }
/*用户域对象*/
@文档(collection=“users”)
公共类用户{
@身份证
私有ObjectId;
@字段(“姓氏”)
私有字符串姓氏;
@字段(“名字”)
私有字符串名;
公共字符串getLast_name(){
返回姓氏;
}
public void setLast_name(字符串last_name){
this.last_name=last_name;
}
公共字符串getFirst_name(){
返回第一个名称;
}
public void setFirst\u name(字符串first\u name){
this.first\u name=first\u name;
}
}
/*UsersRepository.java主界面*/
@存储库
公共接口UsersRepository扩展了MongoRepository,UsersRepositoryCustom{
列出FindUserById(字符串id);
}
/*UsersRepositoryCustom.java自定义接口*/
@存储库
公共接口用户存储自定义{
列出getUsersName(用户);
}
/*UsersRepositoryImpl.java自定义接口实现*/
@组成部分
公共类UsersRepositoryImpl实现UsersRepositoryCustom{
@自动连线
MongoOperations MongoOperations;
@凌驾
公共列表getUsersName(用户){
return mongoOperations.find(
Query.Query(Criteria.where(“first_name”).is(users.getFirst_name())和(“last_name”).is(users.getLast_name()),users.class);
}
/*Spring JUnit测试类中的Mongo测试函数使用主UsersRepository接口调用自定义函数*/
@自动连线
私人用户Repository用户Repository;
@试验
public void getUsersName(){
用户=新用户();
users.setFirst_name(“James”);`在此处输入代码`
users.setLast_name(“奥利弗”);
List usersDetails=usersRepository.getUsersName(用户);
System.out.println(“用户列表”+usersDetails.size());
Assert.assertTrue(usersDetails.size()>0);
}

存储库界面中的查询方法声明无效。如中所述,对于自定义存储库,查询方法需要以
get…By
read_By
find…By
query…By
开头,不需要Oliver所述的方法命名约定。我已经使用名为updateMessageCount的方法挖掘

话虽如此,我看不出这里提供的代码有什么问题

我在这篇文章的帮助下解决了这个问题,因为我没有正确命名Impl类:


自定义存储库不能像文档中提到的那样工作-它可以工作。看起来实现类名不正确。感谢Oliver提供的链接。我们是否也必须遵循自定义方法的查询方法声明。我想您可以声明自定义方法,以searchBy…开头,或者像我上面的示例那样。t应该是什么在上面的例子中,如果域类中有一个带下划线的字段名,例如first_name和setter/getter方法,则使用自定义查询方法声明getFirstName/setFirstName(String firstName)如果这对您有意义,请接受正确的答案:)。如果我在mongodb和我的映射域类中都有一个带下划线的字段名(例如first_name),那么自定义查询方法声明应该是什么呢?您可以通过添加另一个下划线来转义下划线,最终得到
findByFirst__name(…)
。由于这些方法调用会泄漏到客户端,我们建议您首先遵守Java命名约定,不要使用下划线。尽管如此,我已经提交了一份文件,希望在参考文档中包含一个关于转义机制的说明。谢谢Oliver。我会按照您的建议遵循Java命名约定。很高兴能够将转义机制添加到参考文档中。