我正在Spring引导项目上使用Spring数据存储库和自定义存储库,但我想为海关添加通用存储库。
这是GenericCustomRepositoy
:
public interface GenericCustomRepositoy<T, ID> {
List<T> findByFields(Class<T> clazz, List<Pair<String, Object>> fieldValue, List<Pair<String, Boolean>> orderBy);
}
@Repository
public class GenericCustomRepositoyImpl<T, ID> implements GenericCustomRepositoy<T, ID> {
@PersistenceContext
protected EntityManager em;
public List<T> findByFields(Class<T> clazz, List<Pair<String, Object>> fieldValues, List<Pair<String, Boolean>> orderBy) {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<T> cq = cb.createQuery(clazz);
final Root<T> root = cq.from(clazz);
........................
return em.createQuery(cq).getResultList();
}
这是我的存储库:
@Repository
public interface AdresseLivraisonRepository extends JpaRepository<AddressEntity, Integer>, GenericCustomRepositoy<AddressEntity, Integer> {
}
当我不运行应用程序时,我会遇到此异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adresseLivraisonRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property fields found for type AddressEntity!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
我做错了什么!
我更改了代码,并添加了一个自定义存储库,该存储库扩展了Generic,但出现了相同的异常。
这是新的存储库:
@Repository
public interface AdresseLivraisonRepository extends JpaRepository<ForwardingAddressEntity, Integer>, AdresseLivraisonCustomRepository {
}
答案 0 :(得分:1)
您的自定义查询方法的名称遵循Spring Data JPA存储库的命名约定。因此,Spring Data JPA尝试在启动期间为其生成查询方法。
尝试将您的方法重命名为其他名称,例如genericFindByFields(...)
答案 1 :(得分:0)
好吧,我通过Spring Data JpaSpecificationExecutor更改了我的泛型类,它做了我想对泛型Advanced Spring Data JPA - Specifications and Querydsl
所做的事情