'Error creating a base repository class

import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.QueryDslJpaRepository;
import org.springframework.data.querydsl.EntityPathResolver;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;

@NoRepositoryBean
public class RepositorySupportImpl<T> extends QueryDslJpaRepository<T, Integer> implements RepositorySupport<T> {

    private EntityManager entityManager;


    public RepositorySupportImpl(JpaEntityInformation<T, Integer> entityInformation, EntityManager entityManager, EntityManager entityManager1) {
    super(entityInformation, entityManager);
    this.entityManager = entityManager1;
    }

    public RepositorySupportImpl(JpaEntityInformation<T, Integer> entityInformation, EntityManager entityManager, EntityPathResolver resolver, EntityManager entityManager1) {
    super(entityInformation, entityManager, resolver);
    this.entityManager = entityManager1;
    }

    @Override
    public EntityManager getEntityManager() {
    return this.entityManager;
    }

@Transactional
@Override
public <S extends T> S save(final S entity) {
    this.getEntityManager().persist(entity);
    return entity;
}

}

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;

import javax.persistence.EntityManager;

public interface RepositorySupport<T> extends JpaRepository<T, Integer>, QueryDslPredicateExecutor<T> {

    EntityManager getEntityManager();

}

in my config class i have repositoryBaseClass = RepositorySupportImpl.class

But I get this error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registerService': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalStateException: No suitable constructor found on class ca.lighthouse.repository.RepositorySupportImpl to match the given arguments: [class org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation, class com.sun.proxy.$Proxy52]. Make sure you implement a constructor taking these



Solution 1:[1]

I had the same problem just now and after some debugging I figured out the solution:

Make sure you only have one EnableJpaRepositories annotation and that it's pointing to the implementation class (not to the interface):

@EnableJpaRepositories(repositoryBaseClass = GenericJpaRepositoryImpl.class)

I hope it helps someone in the future ;)

Solution 2:[2]

I already had the same problem, and the solution was to create the right constructor for implementing the repository.

    public RepositorySupportImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
    super(entityInformation, entityManager);
}

Solution 3:[3]

You have to use both constructors in the Impl class:

 public GenericRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
    super(entityInformation, entityManager);
    this.em = entityManager;
    domainClass = null;
}


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

Solution 4:[4]

Thanks for all your comments, its working now. I wish I could definitively pin point the problem, but I can't. Here is the scenario, I have a spring mvc application, with controllers, entities, business services, jsps, and I wanted to clean things up so I decided to break the project down into modules. So I created a new project, added the modules and then copied the files from the old project into the new project. When I copied the RepositorySupport class and interface I thought I should rename it, to what you see above. That resulted in this error and after a few days of researching and trying different things I decided to copy in the original files and it worked. That's all I did, copy in the files and updated the references.

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 Fabian Mendez
Solution 2
Solution 3 Tiago PC
Solution 4 Jason