您的位置:首页 > 移动开发

org.springframework.data.mapping.PropertyReferenceException: No property xxxx found for type Xxxx

2017-04-08 15:38 543 查看

解决方法

解决方法详见:spring data jpa @Entity property not found exception



上面大致意思

你为User实体类定义了一个UserRepository,这个UserRepository中的findByName(String name);方法是通过
`name`
(方法的参数位置)去查询数据。但是这个
`name`
并不是User实体类的属性(
XXX
is not a property of your Domain class)。(不知道小弟有没有表达清楚,没有明白的请点击这里查看原文>_<)

解决办法有两种

1、把参数名称改成实体中的属性。比如本例中User实体类中有
firstname
lastname
这两属性,就可以把public User findByName(String name);方法的参数
`name`
改成
firstname
lastname


2、也有可能是一不小心,把实体类中的属性名称写错了哦(见下图)



3、如果是使用主键进行查询的,可以这样式儿的:



这个解决方法有没有用?



为啥会出现这个错误?



好了,下面开始记录解决这个问题的经历:

事情的起因

朋友发给我一段异常信息是这样式儿的:

Error creating bean with name 'customerPackageServiceImpl':
Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field:
private com.fuze.erp.service.customer.ICustomerService com.fuze.erp.service.customer.impl.CustomerPackageServiceImpl.iCustomerService;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'customerServiceImp':
Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field:
private com.fuze.erp.repository.customer.CustomerRepository com.fuze.erp.service.customer.impl.CustomerServiceImp.customerRepository;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'customerRepository':
Invocation of init method failed;
nested exception is org.springframework.data.mapping.PropertyReferenceException:
No property customerId found for type Customer!

然后我就定位到了最后一句话: No property customerId found for type Customer!然后:



按下按钮后,百度上竟然没有相关文章。去谷歌搜了一下,我去。。竟然也没有!!几近疯狂,最后还是在谷歌中找到了线索:(最后折腾那么就的原因 是我错误的理解了报错信息)




参考

org.springframework.data.mapping.PropertyReferenceException: No property detachItem found for type Fruit

Working with Spring Data Repositories

Spring Data JPA - “No Property Found for Type” Exception

Spring Data for Mongo 方法名规范介绍
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SpringData
相关文章推荐