Spring Data MongDB 2.1.0中的PropertyReferenceException

时间:2018-12-19 04:18:27

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

我正在使用spring数据mongdb 2.1.0。 并具有如下所示的自定义存储库实现:

public class ProductItemRepositoryImpl implements ProductItemRepositoryCustom {


  @Override
  public List<String> getItemIdsGivenSkuOrCode(String itemIdType, String itemId) {
    Query query = new Query();
    query.addCriteria(Criteria.where(itemIdType).is(itemId));
    return mongoTemplate
        .findDistinct(query, FieldNames.PRODUCT_ITEM_ID, ProductItem.COLLECTION_NAME,
            String.class);
  }
} 

自定义存储库:

public interface ProductItemRepositoryCustom {
  List<String> getItemIdsGivenSkuOrCode(String itemIdType, String itemId);
}

回购:

public interface ProductItemRepository
    extends MongoRepository<ProductItem, Long>, ProductItemRepositoryCustom {
}

我不明白为什么它将自定义方法(getItemIdsGivenSkuOrCode)作为属性。 当我运行此命令时,出现以下给定错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productItemRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property getItemIdsGivenSku found for type ProductItem!
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:740) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]

1 个答案:

答案 0 :(得分:0)

我认为它必须与方法命名有关

假设您在var: SUN 中的字段是

ProductItem

方法名称应为

String itemIdType;
String itemId;

或者为了安全起见,您可以添加查询

public interface ProductItemRepositoryCustom {
  List<String> findByItemIdTypeOrItemId(String itemIdType, String itemId);
}