Реализация пользовательского метода в Spring Data завершается с ошибкой свойства not found - PullRequest
1 голос
/ 10 октября 2019

Я пытаюсь реализовать пользовательский метод в репозитории 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. ВЫПУСКНАЯ документация

Ответы [ 2 ]

3 голосов
/ 10 октября 2019

Я только что проверил ваш код и смог его исправить. Это то, что я сделал

Переименуйте MyEntityRepositoryCustomImpl в MyEntityRepositoryImpl и

Как я уже говорил в своем комментарии, хранилище cutom должно называться MyEntityRepositoryCustom (я полагаю, вы уже сделали это)

Соглашение об именах является ключевым здесь. Класс Impl должен быть назван <BaseRepository>Impl. И не <CustomRepository>Impl

0 голосов
/ 10 октября 2019

Если вы хотите, чтобы ваш текущий код работал, тогда вам нужен @NamedQuery, если вы хотите запустить hql или если вы хотите выполнить собственный запрос @NamedNativeQuery с именем MyEntity.myCustomFindQuery в вашем классе Entity

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

или

  @NamedNativeQuery(name="MyEntity.myCustomFindQuery", 
    query="SELECT 1 as a, 'a' as b", resultSetMapping="mapmyCustomFindQuery")

@SqlResultSetMapping(name = "mapmyCustomFindQuery", classes = {
        @ConstructorResult(targetClass = MyEntity.class, columns = {
                @ColumnResult(name = "a"), @ColumnResult(name = "b")

        })
})

В качестве альтернативы вы можете хранить оба хранилища раздельно (значит, 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);
            }
        };
    }
...