'Spring data repository not found at compile time

I am trying to use Spring data and repositories in a Spring Boot application, but I have an error when compiling the project.

Here is my Entity :

package fr.investstore.model;

import javax.persistence.Id;
...

@Entity
public class CrowdOperation {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long id;

    @Enumerated(EnumType.STRING)
    public RepaymentType repaymentType;

    ...
}

And the corresponding Repository:

package fr.investstore.repositories;

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

import fr.investstore.model.CrowdOperation;


public interface CrowdOperationRepository extends CrudRepository<CrowdOperation, Long> {

}

I use it in a WS controller, generating a repository through the Autowired annotation:

package fr.investstore.ws;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
...

@Controller
@EnableAutoConfiguration
public class SampleController {

    @Autowired
    private CrowdOperationRepository crowdOperationRepository;


    @RequestMapping(path = "/", method = RequestMethod.GET)
    @ResponseBody
    public String getOperations(@RequestParam(required=true, defaultValue="Stranger") String name) {
        crowdOperationRepository.save(new CrowdOperation());
        return "Hello " + name;
    }
}

And the code of the application:

package fr.investstore;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import fr.investstore.ws.SampleController;

@SpringBootApplication
public class InvestStoreApplication {

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

But when compiling the project I get:


APPLICATION FAILED TO START


Description: Field crowdOperationRepository in fr.investstore.ws.SampleController required a bean of type 'fr.investstore.repositories.CrowdOperationRepository' that could not be found.

Action: Consider defining a bean of type 'fr.investstore.repositories.CrowdOperationRepository' in your configuration.

Woudn't Spring automatically generate a bean for the repository through the interface? How can I resolve this?

EDIT: I also tried to put the Repository annotation (from org.springframework.stereotype.Repository) onto CrowdOperationRepository, but I got the same error



Solution 1:[1]

Thank to @JBNizet for his comment, that made it working.

I create this answer since he did not:

Replace SpringApplication.run(SampleController.class, args); with SpringApplication.run(InvestStoreApplication.class, args);. And remove the useless @EnableAutoConfiguration on your controller.

Solution 2:[2]

While creating a spring-boot application, we need to keep some point in our mind like

  1. Always keep main class (class with `@SpringBootApplication annotation) on the top level package and other classes should lie under sub-packages.

  2. Always mark your bean classes with proper annotation e.g. all repositories should be marked by @Repository annotation, all service implementation classes should be marked with @Service, other component classes should be marked by @Component, class which defines our beans should be marked as @Configuration

  3. Enable the feature which you are using e.g. @EnableJpaRepositories, @EnableTransactionManagement, @EnableJpaAuditing, these annotations also provides functionality which let us define which package spring needs to scan.

So in your case, you need to mark InvestStoreApplication class with @EnableJpaRepositories annotation and CrowdOperationRepository with @Repository.

Solution 3:[3]

you have to tell your spring boot application to load JPA repositories.

copy this one to your application class

it will auto-scan your JPA repository and load it in your spring container even if you do not define your interface with @Repository it will wire that bean in your dependent class.

@EnableJpaRepositories(basePackages = { "fr.investstore.repositories" })

Solution 4:[4]

Annotating your entity class as shown as spring hint below to allow spring get a valid repository bean

Spring Data JPA - Could not safely identify store assignment for repository candidate interface com.xxxxx.xxxxRepository. If you want this repository to be a JPA repository, consider annotating your entities with one of these annotations: javax.persistence.Entity, javax.persistence.MappedSuperclass (preferred), or consider extending one of the following types with your repository: org.springframework.data.jpa.repository.JpaRepository. 2022-05-06 12:32:12.623 [ restartedMain] INFO [.RepositoryConfigurationDelegate:201 ] - Finished Spring Data repository scanning in 3 ms. Found 0 JPA repository interfaces.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Romain Guidoux
Solution 2
Solution 3
Solution 4 Joshua Lai