1
我想一個方法添加到所有JPA庫和我跟着manual並創建了以下類:春數據 - 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
任何想法?