0

I'm trying to add custom repository methods in Spring as show in the documentation here:

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations

and in a Stack Overflow question here:

How to add custom method to Spring Data JPA

and it's not working as expected.

I took the repository, defined as:

public interface ScreenshotRepository extends JpaRepository<Screenshot, UUID> {

and changed it to

public interface ScreenshotRepository extends JpaRepository<Screenshot, UUID>, ScreenshotRepositoryCustom {

Then I created the ScreenshotRepositoryCustom interface:

interface ScreenshotRepositoryCustom {
    void foo();
}

and the implementation:

class ScreenshotRepositoryCustomImpl implements ScreenshotRepositoryCustom {
    @Override
    public void foo() {
        System.out.println("Foo!!!");
    }
}

When I try to start the spring application, I get this message:

Error creating bean with name 'screenshotRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property foo found for type Screenshot!

which I'm having trouble understanding. What's going on here?

Roland Weisleder
  • 9,668
  • 7
  • 37
  • 59
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • You should also post your Screenshot class as this is the one missing the property foo. – java-addict301 Nov 20 '17 at 17:11
  • I don't see any annotations on `ScreenshotRepositoryCustomImpl`. It should be a bean. – Ilya Novoseltsev Nov 20 '17 at 19:03
  • 1
    @java-addict301: the Screenshot class doesn't have and shouldn't have a foo member. The problem was strictly on the repositories and the answer I was pointed to actually contained the solution. – Pablo Fernandez Nov 23 '17 at 11:34
  • @IlyaNovoseltsev: I do get a warning from IntelliJ about that. How do I make it a bean? I tried @ Repository (as well as others) and all of those cause one error or another. – Pablo Fernandez Nov 23 '17 at 11:35
  • @Pablo Hector marked this question as a duplicate and there is a correct answer in his link. You have to name your class `ScreenshotRepositoryImpl` without `Custom` and, yes, make it a `@Repository`. Interface should remain `ScreenshotRepositoryCustom `. That is a correct setup. – Ilya Novoseltsev Nov 23 '17 at 12:04
  • @IlyaNovoseltsev: it's not working for me, I posted another question about it: https://stackoverflow.com/questions/47455561/how-to-annotate-a-custom-spring-boot-custom-repository – Pablo Fernandez Nov 23 '17 at 12:29

0 Answers0