Search code examples
javaspringspring-bootspring-data-jpaspring-data

Custom method implementation in Spring Data fails with property not found error


I'm trying to implement custom method in Spring Data repository using Spring Boot 1.5.9.RELEASE. I created the repository:

package com.example.springdatademo;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
interface MyEntityRepository extends JpaRepository<MyEntity, String>, CustomMyEntityRepository {

}

Provided the custom repository:

package com.example.springdatademo;

interface CustomMyEntityRepository {
    MyEntity myCustomFindQuery();
}

And the implementation:

package com.example.springdatademo;

import org.springframework.stereotype.Component;

    @Component
    class CustomMyEntityRepositoryImpl implements CustomMyEntityRepository {


        @Override
        public MyEntity myCustomFindQuery() {
            System.out.println("hello from custom query implementation");
            return null;
        }
    }

Plus, I provided an invocation:

package com.example.springdatademo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EntityScan
@EnableJpaRepositories
@SpringBootApplication
public class SpringDataDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDataDemoApplication.class, args);
    }

    @Bean
    public CommandLineRunner run(MyEntityRepository repository) {
        return (args) -> {
            final MyEntity myEntity1 = repository.myCustomFindQuery();
            repository.save(new MyEntity(1, "fieldTwo"));
            for (MyEntity myEntity : repository.findAll()) {
                System.out.println(myEntity);
            }
        };
    }

}

pom.xml is just plain one generated from spring initializer:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
<!--        <version>2.1.9.RELEASE</version>-->
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-data-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-data-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

When running the project on Spring Boot 1.5.9.RELEASE I'm getting a problem on container creation: Caused by: java.lang.IllegalArgumentException: Failed to create query method public abstract com.example.springdatademo.MyEntity com.example.springdatademo.CustomMyEntityRepository.myCustomFindQuery()! No property myCustomFindQuery found for type MyEntity!

Changing the Spring Boot version to 2.1.9.RELEASE works fine and gives me the expected result.

I can't find any tips in spring-data-jpa-1.11.9.RELEASE documentation


Solution

  • I just checked out your code and was able to fix it. This is what I did

    Rename MyEntityRepositoryCustomImpl to MyEntityRepositoryImpl and

    As I told you in my comment, cutom repository should be named MyEntityRepositoryCustom(I guess you already did this)

    Naming convention is the key here. Impl class should be named <BaseRepository>Impl. And not <CustomRepository>Impl