cannot set up H2 JPA database witih Spring Boot - error : no bean available to autowire

134 Views Asked by At

I followed the tutorial on Building a REST API with spring boot. https://spring.io/guides/tutorials/rest/

Everything is fine until when I try to set up a @Configuration to set up h2 database connectivity.

I have a repository


import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
interface EmployeeRepository extends JpaRepository<Employee, Long> {}

I have an @Entity :


@Entity
public class Employee {
    private @Id @GeneratedValue Long id;
    private String name;
    private String role;

    public Employee() {}
}

and of course I have my @SpringBootApplication


@SpringBootApplication
public class RestdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(RestdemoApplication.class, args);
    }

}

The error I am getting is in the LoadDatabase @Configuration class :

enter image description here

Why would there be an error when I followed the tutorial exactly? Is the tutorial missing some key step or annotation, or did I goof up somewhere?

1

There are 1 best solutions below

1
On

Your repository class needs to be annotationed with @Repository so it can be registered as a bean inside the Spring Application Context:

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {}

Also, the error in your screenshot is an IDE error, trying to predict a runtime exception on boot. In some cases, it might be a false positive.