title: Spring Data Repository StackOverflow
body: <p>Found some strange behavoir when working with Spring Data Repositories.</p>
  
  <p>I wrote these classes and interfaces:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Transactional(readOnly = true)
  public interface UserRepository extends Repository&lt;User, Integer&gt; {
  
      @Transactional
      @Modifying
      @Query("DELETE FROM User u WHERE u.id=:id")
      int delete(@Param("id") int id);
  
      @Transactional
      User save(User user);
  
      User findOne(Integer id);
  
      List&lt;User&gt; findAll();
  }
  
  
  public interface AbstractRepository&lt;T&gt; {
  
      T save(T user);
  
      // false if not found
      boolean delete(int id);
  
      // null if not found
      T get(int id);
  
      List&lt;T&gt; getAll();
      }
  
  
  
  @Repository
  public class UserRepositoryImpl implements AbstractRepository&lt;User&gt; {
  
      @Autowired
      private JpaUserRepository repository;
  
      @Override
      public User save(User user) {
          return repository.save(user);
      }
  
      @Override
      public boolean delete(int id) {
          return repository.delete(id) != 0;
      }
  
      @Override
      public User get(int id) {
          return repository.findOne(id);
      }
  
      @Override
      public List&lt;User&gt; getAll() {
          return repository.findAll();
      }
  }
  </code></pre>
  
  <p>When I try to test UserRepositoryImpl, java.lang.StackOverflowError is thrown</p>
  
  <pre><code>Caused by: java.lang.StackOverflowError
      at org.springframework.data.repository.util.ClassUtils.unwrapReflectionException(ClassUtils.java:166)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:505)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:478)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:115)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
      at com.sun.proxy.$Proxy55.save(Unknown Source)
      at ru.emitrohin.votingsystem.repository.UserRepositoryImpl.save(RestaurantRepositoryImpl.java:24)
      at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
      at  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
  </code></pre>
  
  <p>I see that there is some problem with save() method. Also stackoverflow is thrown on delete() method.</p>
  
  <p>I've already found the solution. My problem disappears when I change name of interface that extends Repository interface to (for example) JpaUserRepository.</p>
  
  <p>So, the question is "What is going on". Why stackoverflow is thrown when interface name that implements spring data repository matches pattern <em>Classname</em>Repository?</p>
  
  <p>my pom.xml </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://maven.apache.org/POM/4.0.0"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;xxx&lt;/groupId&gt;
      &lt;artifactId&gt;xxx&lt;/artifactId&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
  
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
      &lt;name&gt;xxx&lt;/name&gt;
      &lt;url&gt;xxx&lt;/url&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
  
          &lt;tomcat.version&gt;8.0.33&lt;/tomcat.version&gt;
          &lt;spring.version&gt;4.3.4.RELEASE&lt;/spring.version&gt;
          &lt;spring-security.version&gt;4.2.0.RELEASE&lt;/spring-security.version&gt;
          &lt;spring-data-jpa.version&gt;1.10.4.RELEASE&lt;/spring-data-jpa.version&gt;
  
          &lt;!-- Logging --&gt;
          &lt;logback.version&gt;1.1.7&lt;/logback.version&gt;
          &lt;slf4j.version&gt;1.7.21&lt;/slf4j.version&gt;
  
          &lt;!--DB--&gt;
          &lt;postgresql.version&gt;9.4.1211&lt;/postgresql.version&gt;
  
          &lt;!--Tests--&gt;
          &lt;junit.version&gt;4.12&lt;/junit.version&gt;
  
          &lt;!-- Hibernate --&gt;
          &lt;hibernate.version&gt;5.2.4.Final&lt;/hibernate.version&gt;
          &lt;hibernate-validator.version&gt;5.3.2.Final&lt;/hibernate-validator.version&gt;
  
          &lt;!--Tools--&gt;
          &lt;ehcache.version&gt;2.10.3&lt;/ehcache.version&gt;
  
      &lt;/properties&gt;
  
      &lt;build&gt;
          &lt;finalName&gt;xxx&lt;/finalName&gt;
          &lt;defaultGoal&gt;package&lt;/defaultGoal&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;${java.version}&lt;/source&gt;
                      &lt;target&gt;${java.version}&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.19.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;argLine&gt;-Dfile.encoding=UTF-8&lt;/argLine&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
  
              &lt;!--  http://stackoverflow.com/questions/4305935/is-it-possible-to-supply-tomcat6s-context-xml-file-via-the-maven-cargo-plugin#4417945 --&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.codehaus.cargo&lt;/groupId&gt;
                  &lt;artifactId&gt;cargo-maven2-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.5.0&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;container&gt;
                          &lt;containerId&gt;tomcat8x&lt;/containerId&gt;
                          &lt;systemProperties&gt;
                              &lt;file.encoding&gt;UTF-8&lt;/file.encoding&gt;
                              &lt;spring.profiles.active&gt;tomcat,datajpa&lt;/spring.profiles.active&gt;
                          &lt;/systemProperties&gt;
                          &lt;dependencies&gt;
                              &lt;dependency&gt;
                                  &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
                                  &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
                              &lt;/dependency&gt;
                          &lt;/dependencies&gt;
                      &lt;/container&gt;
                      &lt;configuration&gt;
                          &lt;configfiles&gt;
                              &lt;configfile&gt;
                                  &lt;file&gt;src/main/resources/tomcat/context.xml&lt;/file&gt;
                                  &lt;todir&gt;conf/Catalina/localhost/&lt;/todir&gt;
                                  &lt;tofile&gt;context.xml.default&lt;/tofile&gt;
                              &lt;/configfile&gt;
                          &lt;/configfiles&gt;
                      &lt;/configuration&gt;
                      &lt;deployables&gt;
                          &lt;deployable&gt;
                              &lt;groupId&gt;com.xxx&lt;/groupId&gt;
                              &lt;artifactId&gt;xxx&lt;/artifactId&gt;
                              &lt;type&gt;war&lt;/type&gt;
                              &lt;properties&gt;
                                  &lt;context&gt;${project.build.finalName}&lt;/context&gt;
                              &lt;/properties&gt;
                          &lt;/deployable&gt;
                      &lt;/deployables&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
      &lt;dependencies&gt;
  
          &lt;!-- Logging with SLF4J &amp; LogBack --&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
              &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
              &lt;version&gt;${logback.version}&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;jcl-over-slf4j&lt;/artifactId&gt;
              &lt;version&gt;${slf4j.version}&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
              &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
              &lt;version&gt;${logback.version}&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Spring --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;commons-logging&lt;/groupId&gt;
                      &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
              &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
              &lt;version&gt;${spring-data-jpa.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;commons-logging&lt;/groupId&gt;
                      &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
  
          &lt;!-- spring security--&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
              &lt;version&gt;${spring-security.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
              &lt;version&gt;${spring-security.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-test&lt;/artifactId&gt;
              &lt;version&gt;${spring-security.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!--hibernate--&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.transaction&lt;/groupId&gt;
              &lt;artifactId&gt;jta&lt;/artifactId&gt;
              &lt;version&gt;1.1&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
              &lt;version&gt;${hibernate-validator.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-ehcache&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;net.sf.ehcache&lt;/groupId&gt;
              &lt;artifactId&gt;ehcache&lt;/artifactId&gt;
              &lt;version&gt;${ehcache.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!--Web--&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
              &lt;artifactId&gt;tomcat-servlet-api&lt;/artifactId&gt;
              &lt;version&gt;${tomcat.version}&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!--Test--&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;${junit.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.mockito&lt;/groupId&gt;
              &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
              &lt;version&gt;2.2.21&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
              &lt;version&gt;2.8.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
              &lt;version&gt;2.8.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-datatype-jsr310&lt;/artifactId&gt;
              &lt;version&gt;2.8.4&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-datatype-hibernate5&lt;/artifactId&gt;
              &lt;version&gt;2.8.4&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
              &lt;artifactId&gt;hamcrest-library&lt;/artifactId&gt;
              &lt;version&gt;1.3&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
  
      &lt;profiles&gt;
          &lt;profile&gt;
              &lt;id&gt;hsqldb&lt;/id&gt;
              &lt;dependencies&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.hsqldb&lt;/groupId&gt;
                      &lt;artifactId&gt;hsqldb&lt;/artifactId&gt;
                      &lt;version&gt;2.3.4&lt;/version&gt;
                  &lt;/dependency&gt;
              &lt;/dependencies&gt;
          &lt;/profile&gt;
      &lt;/profiles&gt;
  
      &lt;dependencyManagement&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-framework-bom&lt;/artifactId&gt;
                  &lt;version&gt;${spring.version}&lt;/version&gt;
                  &lt;type&gt;pom&lt;/type&gt;
                  &lt;scope&gt;import&lt;/scope&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
  &lt;/dependencyManagement&gt;
  </code></pre>
  
  <p></p>