我想向所有 JPA 存储库添加一个方法,我按照手册创建了以下类:
@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
有任何想法吗?