Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CrudRepository inside my custom repository implementation

I am attempting to get a reference to my repository interface (UserRepository) that extends CrudRepository within my custom implementation (UserRepositoryExtensionImpl) in order to gain access to all the methods provided by Spring JPA.

Crud Extension:

@Repository
public interface UserRepository extends CrudRepository<User, String>, UserRepositoryExtension<RosterUser> {
    ...any custom spring JPA methods...
}

Extension Interface:

@Repository
public interface UserRepositoryExtension <T> {
   public T put(T entity);
}

Custom Implementation:

public class UserRepositoryExtensionImpl implements UserRepositoryExtension<User> {

    UserRepository userRepository;

    @Autowired
    public UserRepositoryExtensionImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public User put(User user) {
        System.out.println(user + "was put");
        // ...put logic here
        return null;
    }...
}

However, I am unable to inject UserRepository since a circular dependency exists (given that UserRepository extends the interface implemented by my UserRepositoryImpl). I am getting the following error:

org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name '    userRepositoryImpl': Requested bean is currently in creation: Is there an unresolvable circular     reference?

A possible, but less than ideal solution would be to inject and EntityManager into UserRepositoryImp, but in that case, I do not have access to any of the Spring JPA methods provided by CrudRepository, or any additional methods that I might have created in UserRepository.

Any suggestions on how to get around this?

Any help would be greatly appreciated.

EDIT: As mentioned in @shelley's answer, I was able to solve this by making 3 changes:

  • Removing the @Repository from UserRepositoryExtensionImpl
  • Renaming UserRepositoryExtensionImpl to UserRepositoryImpl. Apparently this makes Spring aware of the implementation's existence. See Spring Doc
  • Removing my constructor and moving the @Autowired to the userRepository field

SUCCESS!

like image 513
Julian B. Avatar asked Feb 27 '13 23:02

Julian B.


People also ask

Does JPA repository extend CrudRepository?

JPA repository extends CrudRepository and PagingAndSorting repository. It inherits some finders from crud repository such as findOne, gets and removes an entity.


2 Answers

A couple small things need to be changed in order for this to work:

  • Remove the @Repository annotation from the custom repository interface (UserRepositoryExtension).

  • The custom repository implementation should actually be named "<StandardRepository>Impl" rather than "<CustomRepository>Impl". In your code example, this should be UserRepositoryImpl instead of UserRepositoryExtensionImpl.

like image 58
shelley Avatar answered Oct 04 '22 21:10

shelley


As shelley pointed out, the naming is really important to make the autowire work. In the example below, I follow the right naming standard for my custom interface and its implementation. But my interface that extended the JpaRepository was named “ItemDao” instead of “ItemRepository”, this resulted in that spring ignored my custom implementation altogether...

OBS!!! Should be "ItemRepository"

@Repository
public interface ItemDao extends JpaRepository<Item, Long>, ItemRepositoryCustom {}

my interface

interface ItemRepositoryCustom {...}

my implementation class

class ItemRepositoryImpl implements ItemRepositoryCustom {...}

If anyone have similar problems, start by following the naming standard that is used in the spring documentation at the link below.

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations

like image 22
Olle89 Avatar answered Oct 04 '22 23:10

Olle89