Spring Boot - Error java.lang.Illegalargumentexception: Not an Managed type

Asked

Viewed 1,429 times

0

I’m having a mistake in Spring and I’m not getting a very good understanding of what’s going on, could you help me?

  • LOG
09:25:43.038 [main] ERROR o.s.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'listarVeiculosController': Unsatisfied dependency expressed through field 'veiculoRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'veiculoRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class br.com.itau.ev9.fuse.model.VeiculoCotacao
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175)
    at br.com.itau.ev9.fuse.App.main(App.java:35)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'veiculoRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class br.com.itau.ev9.fuse.model.VeiculoCotacao
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1128)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
    ... 19 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not an managed type: class br.com.itau.ev9.fuse.model.VeiculoCotacao
    at org.hibernate.ejb.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:200)
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:70)
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:67)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:152)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:99)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:199)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:266)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:252)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
    ... 29 common frames omitted
  • listVeiculosController
@Controller
public class ListarVeiculosController {

    @Autowired
    VeiculoRepository veiculoRepository;

    public void request(Exchange exchange) {
        try {
            RetornoData data = new RetornoData();
            List<BuscarVeiculosDTO> listaResponse = new ArrayList();
            List<VeiculoCotacao> listaVeiculos = new ArrayList();

            int anoModelo = Integer.parseInt(exchange.getProperty("year").toString());
            String stringPesquisa = exchange.getProperty("search_terms").toString();
            Boolean zeroKm = Boolean.parseBoolean(exchange.getProperty("zero_km").toString());

            //listaVeiculos = (List<VeiculoCotacao>) veiculoRepository.findBySearchTerms(anoModelo, stringPesquisa);
            listaVeiculos = (List<VeiculoCotacao>) veiculoRepository.findAll();

            for (VeiculoCotacao veiculoCotacao : listaVeiculos) {
                BuscarVeiculosDTO veiculo = new BuscarVeiculosDTO();

                veiculo.setCodigoMolicar(veiculoCotacao.getCdMolicar());
                veiculo.setMarca(veiculoCotacao.getMarca());
                veiculo.setModelo(veiculoCotacao.getModelo());

                listaResponse.add(veiculo);
            }

            data.setData(listaResponse);
            exchange.getIn().setBody(data);

        } catch (Exception e) {
            exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, HttpStatus.INTERNAL_SERVER_ERROR);
            exchange.setException(new VeiculoException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()));
        }
    }
}
  • Veiculocotacao.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class VeiculoCotacao {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    private int idICarros;
    private int anoInicial;
    private int anoFinal;
    private String marca;
    private String modelo;
    private String cdMolicar;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public int getIdICarros() {
        return idICarros;
    }
    public void setIdICarros(int idICarros) {
        this.idICarros = idICarros;
    }
    public int getAnoInicial() {
        return anoInicial;
    }
    public void setAnoInicial(int anoInicial) {
        this.anoInicial = anoInicial;
    }
    public int getAnoFinal() {
        return anoFinal;
    }
    public void setAnoFinal(int anoFinal) {
        this.anoFinal = anoFinal;
    }
    public String getMarca() {
        return marca;
    }
    public void setMarca(String marca) {
        this.marca = marca;
    }
    public String getModelo() {
        return modelo;
    }
    public void setModelo(String modelo) {
        this.modelo = modelo;
    }
    public String getCdMolicar() {
        return cdMolicar;
    }
    public void setCdMolicar(String cdMolicar) {
        this.cdMolicar = cdMolicar;
    }



}
  • Veiculorepository.java
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import br.com.itau.ev9.fuse.model.VeiculoCotacao;

@Repository
public interface VeiculoRepository extends CrudRepository<VeiculoCotacao, Integer>{


}
  • You can also put your configuration class?

1 answer

2


For his stacktrace:

Caused by: java.lang.IllegalArgumentException: Not an managed type: class br.com.itau.ev9.fuse.model.VeiculoCotacao

What happens is that your class VeiculoCotacao is not being recognised as a persistence entity, probably due to lack of configuration.

You can indicate the package where this entity is via the following annotation in your application/configuration:

@EntityScan(basePackages = {"br.com.itau.ev9.fuse.model"})

It is important to note that if you have other packages with entities and they are not below the hierarchy of your application class, they should be referenced in the array above.


Including also the link to documentation of @EntityScan:

Configures the base Packages used by auto-Configuration when Scanning for Entity classes.

Using @Entityscan will cause auto-Configuration to:

Set the packages scanned for JPA entities.
Set the packages used with Neo4J's SessionFactory.
Set the initial entity set used with Spring Data MongoDB, Cassandra and Couchbase mapping contexts.

One of basePackageClasses(), basePackages() or its alias value() may be specified to define specific Packages to scan. If specific Packages are not defined Scanning will occur from the package of the class with this Annotation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.