我正在尝试使用 Spring Boot 1.5.9.RELEASE 在 Spring 数据存储库中实现自定义方法。 我创建了存储库:
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
只是从 spring 初始化程序生成的普通文件:
<?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 上运行项目时,我遇到了容器创建问题:
原因:java.lang.IllegalArgumentException:无法创建查询方法 public abstract com.example.springdatademo.MyEntity com.example.springdatademo.CustomMyEntityRepository.myCustomFindQuery()!找不到类型 MyEntity 的属性 myCustomFindQuery!
将 Spring Boot 版本更改为 2.1.9.RELEASE 效果很好,并给出了预期的结果。
我在 spring-data-jpa-1.11.9.RELEASE documentation 中找不到任何提示
最佳答案
我刚刚检查了您的代码并能够修复它。这就是我所做的
重命名MyEntityRepositoryCustomImpl
至MyEntityRepositoryImpl
和
正如我在评论中告诉你的,自定义存储库应命名为 MyEntityRepositoryCustom
(我猜你已经这样做了)
命名约定是这里的关键。 Impl 类应命名为 <BaseRepository>Impl
。而不是<CustomRepository>Impl
关于java - Spring Data 中的自定义方法实现失败并出现属性未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58320209/