Я пытаюсь реализовать пользовательский метод в репозитории Spring Data, используя Spring Boot 1.5.9.RELEASE. Я создал репозиторий:
package com.example.springdatademo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
interface MyEntityRepository extends JpaRepository<MyEntity, String>, CustomMyEntityRepository {
}
Предоставил пользовательский репозиторий:
package com.example.springdatademo;
interface CustomMyEntityRepository {
MyEntity myCustomFindQuery();
}
И реализация:
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;
}
}
Плюс я предоставил вызов:
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
- это просто один, сгенерированный из инициализатора пружины:
<?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>
При запуске проекта в Spring Boot 1.5.9.RELEASE У меня возникает проблема при создании контейнера: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!
Изменение версии Spring Boot на 2.1.9.RELEASE работает нормально и дает ожидаемый результат.
Не могу найти никаких подсказок в spring-data-jpa-1.11.9. ВЫПУСКНАЯ документация