13014

Spring Data - JPA, customizing Repository not working

Question:

I want to add a method to all JPA repositories and I followed the <a href="http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/#repositories.custom-implementations" rel="nofollow">manual</a> and created the following classes:

@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; } } }

Also I modified repository configuration:

<repositories base-package="xx.yy.billing.domain.repository.jpa" entity-manager-factory-ref="entityManagerFactory" factory-class="xx.yy.billing.domain.repository.jpa.BillingEntityJPARepositoryFactoryBean"/>

And finally I made all my repository interfaces extend BillingEntityJPARepository, now whenever I use any repository everything works fine unless I call the method defined in BillingEntityJPARepository (findAllWithoutNullableObject) it gives me the following exception:

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

Any ideas?

Answer1:

Its a stupid mistake I forgot the public modifier in BillingEntityJPARepository declaration.

Recommend

  • Yii2 - ListView search form
  • How do you copy a datetime field from the current document to a new document
  • Symfony Bundle: how to avoid using __DIR__ to build the path to Doctrine mappings
  • Why doesn't the following regex work when input through Spring
  • Can I use prePersist/preUpdate in Sonata Admin controller to persist more than one object?
  • python regex split string while keeping delimiter with value
  • Problem in Creating a View Dynamically ( in Android ) [Error : couldn't save which view …]
  • onActivityResult() not called after startActivityForResult() with Intent.ACTION_GET_CONTENT
  • Store additional information during registration with Firebase in Android
  • Shuffling an Array in Android
  • How to add an item to a ListView with each Button click
  • SQLiteOpenHelper: onCreate() method not called on physical device
  • When is locking on types a good idea?
  • android-How to enable cache or buffering when videoView have downloaded a video
  • Django Migrations fail during django initialization
  • Change border corlor of NSTableView
  • Animate a CAShapeLayer's subclass custom property based on the path property
  • Listview - list item selection and initial setting (custom made items)
  • redirect user after update in class based view in django
  • Retrieving a double from a JTextArea while solving for X
  • MRO with multiple inheritance in python
  • Extending the Django 1.11 User Model
  • Fully customized Python Help Usage
  • Swift Initialization Rule Confusion
  • How to upload specific List image using click on Upload button
  • What's the syntax to inherit documentation from another indexer?
  • Blackberry 6: how to detect a long click on track pad?
  • Zoom in and out of jPanel
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • how to avoid repetitive constructor in children
  • GridView breaks while scrolling
  • NHibernate Validation Localization with S#arp Architecture
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Deleting and Updating values from a cusrsor adapter
  • Possible to stop flickering java tooltip in heavyweight mode?
  • unknown Exception android
  • failed to connect to specific WiFi in android programmatically