1
resposta

java api persitencia Spring boot - queries derivadas

Ola podem me ajudar? nao esta iniciando a aplicacao no Spring

Na classe Principal.java

codigo:
    private void buscarSeriesTop5() {
        List<Serie> seriesTop = repositorio.findTop5ByOrderbyAvaliacao();
        seriesTop.forEach(t -> System.out.println(t.getTitulo()));
    };
no Serierepository.java
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'screenmatchApplication': Unsatisfied dependency expressed through field 'repositorio': Error creating bean with name 'serieRepository' defined in br.com.alura.screenmatch.repository.SerieRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Could not create query for public abstract java.util.List br.com.alura.screenmatch.repository.SerieRepository.findTop5ByOrderbyAvaliacao(); Reason: Failed to create query for method public abstract java.util.List br.com.alura.screenmatch.repository.SerieRepository.findTop5ByOrderbyAvaliacao(); No property 'orderbyAvaliacao' found for type 'Serie'
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:714) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:694) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:483) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941) ~[spring-context-6.0.10.jar:6.0.10]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.10.jar:6.0.10]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.1.jar:3.1.1]
at br.com.alura.screenmatch.ScreenmatchApplication.main(ScreenmatchApplication.java:17) ~[classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serieRepository' defined in br.com.alura.screenmatch.repository.SerieRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Could not create query for public abstract java.util.List br.com.alura.screenmatch.repository.SerieRepository.findTop5ByOrderbyAvaliacao(); Reason: Failed to create query for method public abstract java.util.List br.com.alura.screenmatch.repository.SerieRepository.findTop5ByOrderbyAvaliacao(); No property 'orderbyAvaliacao' found for type 'Serie'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770) ~[spring-beans-6.0.10.jar:6.0.10]
1 resposta

Olá, Fabio! Tudo bem?

Parece que o Spring Data JPA está tendo problemas para criar a consulta derivada findTop5ByOrderbyAvaliacao(). Isso está acontecendo porque ele não consegue encontrar a propriedade 'orderbyAvaliacao' na entidade 'Serie'.

As consultas derivadas do Spring Data JPA são montadas com base nos nomes dos métodos, então é importante que o nome do método esteja correto e corresponda aos campos da entidade. No seu caso, parece que você quer buscar as 5 séries com as melhores avaliações, então acredito que você queira ordenar as séries pela avaliação.

Se a entidade 'Serie' tiver um campo chamado 'avaliacao', você pode corrigir o problema alterando o nome do método para findTop5ByOrderByAvaliacaoDesc(). O 'OrderBy' é a palavra-chave do Spring Data JPA para ordenar os resultados, e 'Desc' é para ordenar de forma decrescente, ou seja, das melhores para as piores avaliações.

Seu código ficaria assim:

private void buscarSeriesTop5() {
    List<Serie> seriesTop = repositorio.findTop5ByOrderByAvaliacaoDesc();
    seriesTop.forEach(t -> System.out.println(t.getTitulo()));
};

Por favor, verifique se o campo 'avaliacao' existe na entidade 'Serie' e se o nome está correto. Se o nome do campo estiver diferente, você deve usar o nome correto no método. Caso isso não solucione o seu problema, peço que compartilhe todo o seu código para testes.

Espero ter ajudado e bons estudos!

Caso este post tenha lhe ajudado, por favor, marcar como solucionado ✓.