欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-05 08:47:21
...

       项目中尝试使用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.