Java Spring数据中的自定义方法实现失败,出现“未找到属性”错误

Java Spring数据中的自定义方法实现失败,出现“未找到属性”错误,java,spring,spring-boot,spring-data-jpa,spring-data,Java,Spring,Spring Boot,Spring Data Jpa,Spring Data,我正在尝试使用SpringBoot1.5.9.0版本在SpringDataRepository中实现自定义方法。 我创建了存储库: package com.example.springdatademo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository interface MyEntityRepo

我正在尝试使用SpringBoot1.5.9.0版本在SpringDataRepository中实现自定义方法。 我创建了存储库:

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>

4.0.0
org.springframework.boot

如果要运行当前代码,则需要@NamedQuery来运行hql 或者,如果要在实体类中运行名为MyEntity.myCustomFindQuery的本机查询@NamedNativeQuery

@NamedQuery(name="MyEntity.myCustomFindQuery", 
query="SELECT 1 as a, 'a' as b from MyEntity")

或者,您可以将这两个存储库分开(意味着MyEntityRepository不应扩展MyEntityRepositoryCustom

在这种情况下,您的应用程序类将如下所示

@Autowired 
    MyEntityRepository repository;

    @Autowired
    MyEntityRepositoryCustom entityRepositoryCustom;



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

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

我刚刚签出了你的代码,并且能够修复它。这就是我所做的

将MyEntityRepositoryCustomImpl重命名为MyEntityRepositoryImpl

正如我在评论中告诉您的,cutom存储库应该命名为
MyEntityRepositoryCustom
(我想您已经这样做了)


命名约定是这里的关键。Impl类应该命名为
Impl
。而不是
Impl

我想接口应该命名为
MyEntityRepositoryCustom
,根据文档,只要有
Impl
后缀,这并不重要。这只是一个快速复制的示例,可以放在Githum上您可能正在查看新文档。您必须检查与正在使用的spring数据版本相关的文档。但请尝试一下我的建议。同时确保所有三个文件都位于同一个包、存储库、customrepository和impl@pvpkiran重命名没有帮助。另外,我提供的链接指的是美国文档的实际版本我不想使用
@NamedQuery
方法-我想扩展Spring数据存储库,然后我建议将自定义存储库分开,因为@repository接口MyEntityRepository扩展了JpaRepository,MyEntityRepository自定义将尝试解决所有定义的方法上的操作这是ext的建议方式结束Spring数据存储库,如文档中所述,您可以在原始问题中添加文档的url吗:啊,我现在明白了-实现必须有基本存储库的名称,而不是自定义存储库的名称。尽管奇怪的是,每个存储库只能有一个扩展类
@Autowired 
    MyEntityRepository repository;

    @Autowired
    MyEntityRepositoryCustom entityRepositoryCustom;



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

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