【问题标题】:Spring Data MongoDB No property get found for type at org.springframework.data.mapping.PropertyPathSpring Data MongoDB 在 org.springframework.data.mapping.PropertyPath 中找不到类型的属性
【发布时间】:2014-05-14 14:33:09
【问题描述】:

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

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

  • 感谢奥利弗的链接。我们是否也必须遵循自定义方法的查询方法声明。我认为您可以声明自定义方法以 searchBy... 开头,或者像我上面的示例一样。在上述情况下,自定义查询方法声明应该是什么,例如,如果我有一个带下划线的字段名称,例如域类中的 first_name 和 setter/getter 方法为 getFirstName / setFirstName(String firstName)
  • 如果这对您有意义,请接受正确的答案:)。
  • 如果我在 mongodb 和我的映射域类中都有一个带下划线的字段名称,例如 first_name,那么自定义查询方法声明应该是什么
  • 你可以通过添加另一个下划线来转义下划线,你最终会得到findByFirst__name(…)。由于这些方法调用会泄漏到客户端中,我们建议宁愿坚持 Java 命名约定,并且首先不要使用下划线。尽管如此,我还是提交了DATACMNS-505 以在参考文档中包含有关转义机制的说明。
  • 谢谢奥利弗。根据您的建议,我将遵循 Java 命名约定。非常适合将转义机制添加到参考文档中。
【解决方案2】:

对于自定义存储库,不需要如 Oliver 所说的方法命名约定。我有一个名为 updateMessageCount 的方法

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

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

No property found for type error when try to create custom repository with Spring Data JPA

【讨论】:

    猜你喜欢
    • 2013-11-04
    • 2017-05-17
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 2016-10-13
    • 2019-01-02
    相关资源
    最近更新 更多