我想在所有JPA存储库中添加一个方法,然后我按照manual创建了以下类:
@NoRepositoryBean
interface BillingEntityJPARepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
/**
* @return
*/
public Set<T> findAllWithoutNullableObject(Class<T> clazz);
}
public class BillingEntityJPARepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements BillingEntityJPARepository<T, ID> {
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager entityManager;
@Autowired
private NullObjectsCache nullObjectsCache;
/**
* @param domainClass
* @param em
*/
public BillingEntityJPARepositoryImpl(Class<T> domainClass, EntityManager em) {
super(domainClass, em);
}
/**
* {@inheritDoc}
*/
@Override
public Set<T> findAllWithoutNullableObject(Class<T> clazz) {
//...
}
}
public class BillingEntityJPARepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable> extends JpaRepositoryFactoryBean<R, T, I> {
/**
* {@inheritDoc}
*/
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
return new BillingEntityJPARepositoryFactory(entityManager);
}
private static class BillingEntityJPARepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {
private EntityManager entityManager;
/**
* @param entityManager
*/
public BillingEntityJPARepositoryFactory(EntityManager entityManager) {
super(entityManager);
this.entityManager = entityManager;
}
/**
* {@inheritDoc}
*/
protected Object getTargetRepository(RepositoryMetadata metadata) {
return new BillingEntityJPARepositoryImpl<T, I>((Class<T>) metadata.getDomainClass(), entityManager);
}
/**
* {@inheritDoc}
*/
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return BillingEntityJPARepository.class;
}
}
}
我也修改了存储库配置:
<repositories base-package="xx.yy.billing.domain.repository.jpa" entity-manager-factory-ref="entityManagerFactory" factory-class="xx.yy.billing.domain.repository.jpa.BillingEntityJPARepositoryFactoryBean"/>
最后我将所有存储库接口扩展为BillingEntityJPARepository,现在每当我使用任何存储库时一切正常,除非我调用BillingEntityJPARepository(findAllWithoutNullableObject)中定义的方法,它给出了以下异常:
Caused by: java.lang.IllegalAccessException: Class org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor can not access a member of class com.colureware.billing.domain.repository.jpa.BillingEntityJPARepository with modifiers "public abstract"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
at java.lang.reflect.Method.invoke(Method.java:594)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:368)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:349)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
... 61 more
有什么想法吗?
答案 0 :(得分:0)
这是一个愚蠢的错误,我忘记了BillingEntityJPARepository声明中的公共修饰符。