3

I am trying to provide a Custom Implementation of repository using Spring Data JPA. I have :

public interface PersonRepositoryCustom{

    List<Person> chercher(String name);

}

And the implementation :

 public class PersonRepositoryImpl implements PersonRepositoryCustom{

        List<Person> chercher(String name){ 
// my implementation
         }

    }

The two classes are in the jpa:repositories package

Here is my Person DAO:

public interface IPersonDAO extends CrudRepository<Person, Long>,PersonRepositoryCustom{ 
// other methods here
}

When i launch the server i am getting the error :

org.springframework.data.mapping.PropertyReferenceException: No property chercher found for type Person
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:201)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:291)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:271)
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:80)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:57)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:91)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:69)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:304)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:161)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:84)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
7
  • You probably need to supply more code. I'd guess you're missing the EnableJparepositories annotation? Or possibly you're missing the Repository annotation on the repository? Commented Aug 13, 2014 at 14:58
  • in the documentation here there is not mention of annotation docs.spring.io/spring-data/jpa/docs/1.5.3.RELEASE/reference/… Commented Aug 13, 2014 at 15:01
  • It's find the repository and the method but spring try to guess which attributes to fetch using the method name of the interface. I think Spring does not see the implementation. Commented Aug 13, 2014 at 15:04
  • 4
    PersonRepositoryImpl should be a class, not an interface. Commented Aug 13, 2014 at 15:30
  • Which version of spring-data-jpa are you using? Commented Aug 13, 2014 at 15:46

1 Answer 1

9

I've found the answer. Spring-data-jpa use a convention to write CustomRepository. To achieve this task : we have to have as explained in the Spring Data JPA documentation . We have to create a interface in which we add custom method :

public interface PersonRepositoryCustom{

    List<Person> chercher(String name);

}

Suppose we have following DAOService :

public interface IPersonDAO extends CrudRepository<Person, Long>,PersonRepositoryCustom{ 
// other methods here
}

So the implementation of custom repository is :IPersonDAOImpl

public class IPersonDAOImpl implements PersonRepositoryCustom{

        List<Person> chercher(String name){ 
// my implementation
         }

    }

And not PersonRepositoryImpl

I hope this will help.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for that, I think they should outline that the implementation as to use the name of the repository itself and not the custom interface.
thanks for pointing to specific part of documentation - what helped me with my problem (having custom repository to be shared among multiple repositories)
Doesn't seem to work for me :(.
Oh man, it also has to be in the same namespace! dumb...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.