java - Spring Data mongdb 2.1.0 中的 PropertyReferenceException
问题描述
我正在使用弹簧数据 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]
解决方案
我认为它必须与方法命名有关
假设您的字段ProductItem
是
String itemIdType;
String itemId;
方法名称将是
public interface ProductItemRepositoryCustom {
List<String> findByItemIdTypeOrItemId(String itemIdType, String itemId);
}
或者为了更安全,您可以添加查询
@Query("{ 'itemIdType': ?0, 'itemId': ?1}")
public List<String> findByItemIdTypeOrItemId(String itemIdType, String itemId);
推荐阅读
- react-native - ReadableNativeMap 不能转换为 java.lang.String
- python - 如何使用 Reportlab 编写选项卡?
- typescript - Typescript - union types
- eclipse - Ctrl + 空格给出错误说“计算附加信息”遇到问题
- docker - 在 docker 容器中针对 Localstack 运行 shell 脚本
- image-processing - 如何平铺单个克隆图像图形魔术
- javascript - 基于 Acrobat 中的表单域值的动态字体颜色分配
- java - 多个指向同一个对象的引用变量是否每个都占用与该对象相同的内存占用
- laravel - 使用 laravel 上传到远程 ftp 服务器后损坏的图像文件
- json - 如何使用 VBA 检索 JSON 响应?