排查spring data实现自定义Repository的一个问题

       项目中尝试使用spring-data作为快速开发工具,今天需要实现一个具体的Repository,然后就自定义了一个,结果报下面的错误。

       

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property update found for type xxx.yyy.zzz.customer.model.Consumer
	at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:74)
	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:326)
	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:352)
	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:352)
	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:306)
	at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
	at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:244)
	at org.springframework.data.repository.query.parser.Part.<init>(Part.java:73)
	at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:180)
	at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:260)
	at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:240)
	at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:68)
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:57)
	at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90)
	at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)
	at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:280)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:148)
	at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125)
	at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
	... 33 more

     从错误来看是spring尝试着去解析这个类了,然后发生了错误,后来发现是命名规范不符合springdata的规范。

 

     springdata的规范如下,要遵从repository-custom-impl的规范,具体如下:

      

public interface ConsumerRepository extends JpaRepository<Consumer, Long>,ConsumerRepositoryCustom {
	List<Consumer> findByCusinessType(int customerType);
}

    我们需要额外实现的一个自定义的方法:

   

public interface ConsumerRepositoryCustom {
	void updateCustomerNumber(Long oldId, Long newId);
}

 

   我们自定义的实现类:

   

public  class ConsumerRepositoryImpl implements ConsumerRepositoryCustom{
	@PersistenceContext
	private EntityManager entityManager;
	
	@Override
	public void updateCustomerNumber(Long oldId,Long newId){
		Query query = entityManager.createQuery("fddffdfdfd");
		query.setParameter(1, newId); 
		query.setParameter(2, oldId);
		
		query.executeUpdate();
	}

}

       在这个地方,我们经常会将实现类写成ConsumerRepositoryCustomImpl, 这样的话就出错了,从命名来看,我们可以看出来,最后说明一下如果我们的Repository是ICustomDao, 那么我们的custom接口应该命名成ICustomDaoCustom,相应的实现类就应该为ICustomDaoImpl.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值