Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend SimpleJpaRepository

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?

like image 450
Ali Ahm... Avatar asked Jul 30 '17 19:07

Ali Ahm...


2 Answers

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"
    }
)

like image 164
JohannesB Avatar answered Sep 26 '22 21:09

JohannesB


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

like image 33
Rahul Singh Avatar answered Sep 22 '22 21:09

Rahul Singh