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

标签 mongodb spring-data mongodb-java spring-data-mongodb

我使用的是 Spring Data MongoDB 1.4.2.Release 版本。对于 Spring Data MongoDB,我在一个位置创建了自定义存储库接口(interface)和实现,并创建了自定义查询函数 getUsersName(Users 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 Data 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);
        }

最佳答案

您的存储库接口(interface)中的查询方法声明无效。正如 reference documentation 中明确指出的那样,查询方法需要以get…Byread_Byfind…Byquery…by开头。

关于mongodb - Spring Data MongoDB 在 org.springframework.data.mapping.PropertyPath 找不到类型的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23657661/

相关文章:

java - 如何从 Spring-Data-JPA 返回单个结果?

performance - MongoDB 中大型集合的批量插入性能

java - 如何动态改变文档结构,从标量值合并到数组中?

mongodb - 国外收藏的查找和排序

node.js - Mongoose 更新或插入许多文件

java - 在扩展 CrudRepository 的接口(interface)中缓存 Pageable 对象(结果)

java - 从 .toString() 判断 BasicMongoDBObject 是否有效?

c# - 使用 NoSQL DB 作为 FileStorage 的建议,以及优点和缺点

node.js - 使用 mongoose 更新具有多个嵌入式文档数组的 mongodb 文档

java - Spring Data - 覆盖某些存储库的默认方法