7

I'm using Spring Boot, when I want to extend SimpleJpaRepository like this interface:

public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID>{}

and this implementation:

public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements BaseRepository<T, ID>
{
    private final EntityManager entityManager;

    public BaseRepositoryImpl(Class<T> domainClass, EntityManager entityManager)
    {
        super(domainClass, entityManager);
        this.entityManager = entityManager;
    }
}

I got the following error:

Could not autowire. No beans of 'Class<T>' type found.

How can I resolve it?

6

2 Answers 2

11

When trying to make an implementation of JpaRepository through extending SimpleJpaRepository, it is important that spring knows you are doing this. By default spring will try to create a SimpleJpaRepository, even if you extend it. Therefore your new implementation of the SimpleJpaRepository will not be populated, created or even remotely available. Therefore all repositories extending your new custom repository will not be created either.

To solve this problem, one should add some repository config. More specifically you need to provide a repositoryBaseClass in the @EnableJpaRepositories annotation; e.g.

@Configuration
@EnableJpaRepositories(
    repositoryBaseClass = RepositoryImpl.class
)

Usually you already have some config class to provide your DataSource-bean, EntityManager-bean and/or JpaTransactionManager-bean. As a general best-practice I suggest placing it over there.

Sometimes spring gets a little confused when you do this and still doesn't manage to find your newly defined repositoryBaseClass. In that case, help the spring repository autowirer a little and also provide a basepackage, e.g.

@Configuration
@EnableJpaRepositories(
    repositoryBaseClass = RepositoryImpl.class,
    basePackages = {
        "be.your.company.your.path.to.your.repositories"
    }
)

2
  • 1
    YOU... ARE... THE... MAN!!! After a million hours trying to figure out this godforsaken problem, I finally hit your answer... I can't upvote this answer enough, but I can do the next best thing.
    – eestein
    Feb 14, 2022 at 13:19
  • 1
    Haha, I remember when I made this answer that I went through quite the journey to find a solution myself. Glad I could be of any help! Happy coding!
    – JohannesB
    Feb 15, 2022 at 17:15
2

Make a interface extending JpaRepository

For eg -

public interface Repository extends JpaRepository<Entity,Integer>,RepositoryCustom // this is our custom repository{


}

Repository Custom is a Interface

public interface RepositoryCustom {


    List<Entity> getAll(); // define the method signature here


}

Implementing the Custom Interface

@Transactional
public class RepositoryImpl implements  RepositoryCustom{

    @PersistenceContext // this will inject em in your class
    private EntityManager entityManager;

    write the method body and return

}

Keep in mind the Repository naming convention . If the Interface name is Repository . then the custom interface shuld be named as Repository and the implementation as Repository

2
  • thank you, but this is "Adding custom behavior to single repositories" (section 1.3.1 of docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/…), i want to implement "Adding custom behavior to all repositories" (section 1.3.2 of above URL)
    – Ali Ahm...
    Jul 31, 2017 at 7:00
  • For a more elaborate answer (including why do we need to create three classes) wrt. all kinds repository customizations, you can also read my other answer here.
    – JohannesB
    Sep 29, 2022 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.