Repository not found when starting Springboot

Asked

Viewed 1,361 times

2

I am running my spring boot project with mysql connection and is giving the following failure:

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.

Man 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);
}

Man 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";
    }

}

mine 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?

  • It seems that the problem may be the Event entity. Are you sure that the @Event Id is a String?

  • It is not locating the repository, which has to do with the entity Event?

  • The class with the main method is in which package? com.eventoapp?

  • Is using the Starter of date, spring-boot-starter-data-jpa? If not, write down your DataConfiguration with @EnableJpaRepositories.

  • How is your entity Evento? She is marked with @Entity? Post her code too.

1 answer

1


There are different reasons for this error to occur.

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

For this, you need to write down your class @EnableJpaRepositories, passing the path of the package where Repository is. Example:

package com.mensagens; // pacote diferente!

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

This mistake too may occur if your entity Evento is not with the annotation @Entity. 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 lot of time to realize that the problem is in the entity.

Not your case, but the lack of @Repository class also generates the same error.

Browser other questions tagged

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