Repository not found when starting SpringBoot

2

I'm running my spring boot project with connection to mysql and it's giving the following crash:

  

Description:

     

Field er in com.eventoapp.controllers.EventoController required a bean   of type 'com.eventoapp.repository.EventoRepository' that could not be   found.

     

Action:

     

Consider defining a bean of type   'com.eventoapp.repository.EventoRepository' in your configuration.

My EventoReposity :

package com.eventoapp.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.eventoapp.models.Evento;

@Repository
public interface EventoRepository extends CrudRepository<Evento, String>{
    Evento findByCodigo(long codigo);
}

My EventoController :

package com.eventoapp.controllers;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.eventoapp.models.Evento;
import com.eventoapp.repository.EventoRepository;

@Controller
public class EventoController {

    @Autowired
    EventoRepository er;

    @RequestMapping(value="/cadastrarEvento", method=RequestMethod.GET)
    public String form() {
        return "evento/formEvento";
    }

    @RequestMapping(value="/cadastrarEvento", method=RequestMethod.POST)
    public String form(Evento evento) {

        er.save(evento);
        return "redirect:/cadastrarEvento";
    }

}

My DataConfiguration :

package com.eventoapp.eventoapp;


import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

@Configuration
public class DataConfiguration {

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/eventosapp");
        dataSource.setUsername("root");
        dataSource.setPassword("hednf8iw");
        return dataSource;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter(){
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(Database.MYSQL);
        adapter.setShowSql(true);
        adapter.setGenerateDdl(true);
        adapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
        adapter.setPrepareConnection(true);
        return adapter;
    }
}

Any idea what it might be?

    
asked by anonymous 13.05.2018 / 16:26

1 answer

0

There are different reasons for this error to occur.

The most common is when your annotated class with @SpringBootApplication is in a package (or module) different from Repository .

For this, you must note its @EnableJpaRepositories class, passing the package path where the Repository is. Example:

package com.mensagens; // pacote diferente!

@EnableJpaRepositories(basePackages = "br.com.mensagens.repository") // pacote onde está meu repository
@SpringBootApplication
public class MensagensApplication {

This error can also occur if your entity is not with the annotation Evento . This error, in this case, is very confusing because you think there is something wrong with your repository (or its location) and it takes a long time to realize that the problem is in the entity.

This is not your case, but the lack of @Entity in the class also generates the same error.

    
14.05.2018 / 22:07