1

すべての 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

何か案は?

4

1 に答える 1

0

BillingEntityJPARepository宣言のパブリックモディファイアを忘れてしまったのはばかげた間違いです。

于 2012-05-27T11:31:33.850 に答える