Results 1 to 6 of 6

Thread: Spring Data: Calling default CRUD methods inside a custom repository implementation?

  1. #1

    Default Spring Data: Calling default CRUD methods inside a custom repository implementation?

    I'm using Spring Data 1.2.0.RELEASE. I'm using Spring Data's "custom implementation repository" functionality, as it is described here: http://static.springsource.org/sprin...mplementations
    These are my interfaces and classes:

    Code:
    interface UserRepositoryCustom {
    
      public void convertThenSave(LegacyUserClass legacyUser);
    }

    Code:
    class UserRepositoryImpl implements UserRepositoryCustom {
    
      public void convertThenSave(LegacyUserClass legacyUser) {
        User convertedUser = convert(legacyUser);
        // now I would like to call UserRepository.save(convertedUser) ... 
      }
    }
    Code:
    public interface UserRepository extends CrudRepository<User, Long>, UserRepositoryCustom {
    
      // Declare query methods here
    }
    As you can see in the above code, inside UserRepositoryImpl, I would like to call one of the CRUD methods inherited from CrudRepository. But because of the inheritance hierarchy as required by Spring Data's "custom implementation repository" functionality, this isn't possible.

    What would be a good pattern that fulfills what I actually want to do?

    My example use case of converting some legacy entity class to the repository's managed entity class may cause some people complain that a conversion service isn't the responsibility of a repository. But there are similar use cases, e.g. where one would like to delegate to the CrudRepository-provided methods from inside SomeRepositoryCustom to follow the DRY principle.
    Last edited by Eduard; Dec 3rd, 2012 at 11:55 AM.

  2. #2
    Join Date
    Jul 2007
    Posts
    6

    Default

    I think the reference link you provided also provides ways to wire references into the custom repository implementation.

    Regards.

  3. #3

    Default

    Quote Originally Posted by okmich View Post
    I think the reference link you provided also provides ways to wire references into the custom repository implementation.
    Do you mean something like this:

    Code:
    class UserRepositoryImpl implements UserRepositoryCustom {
    
        @Inject
        private UserRepository userRepository;
    
        @Inject 
        public UserRepositoryImpl(UserRepository userRepository) {
            this.userRepository = userRepository;
    
        public void convertThenSave(LegacyUserClass legacyUser) {
            User convertedUser = convert(legacyUser);
            // now I would like to call this.userRepository.save(convertedUser) ... 
        }
    }
    No, unfortunately this doesn't work:

    My application crashes during container creation time, throwing an exception, pointing out to a circular bean creation problem (the UserRepositoryImpl object depends on the auto-created UserRepository object which depends on the UserRepositoryImpl object which depends on ....). (UnsatisfiedDependencyException and BeanCurrentlyInCreationException ).

  4. #4
    Join Date
    Dec 2010
    Posts
    10

    Default

    I have a similar problem, because I want to create a WriteOnceRepository overwriting save* and disabling delete*-Methods.

    Disabling works just fine as pointed out in the reference Documentation, but trying to "override" save using the CrudRepositroy.save (simply checking for existence before save), I haven't been able to do, trying similar approaches as mentioned above.

  5. #5
    Join Date
    Feb 2013
    Posts
    1

    Unhappy

    Hi all,

    has anyone been able to get around this?

    I am also experiencing this issue.

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

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

    An alternative solution to this problem 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?

  6. #6
    Join Date
    Oct 2009
    Posts
    20

    Default

    Quote Originally Posted by Eduard View Post
    I would like to call one of the CRUD methods inherited from CrudRepository. But because of the inheritance hierarchy as required by Spring Data's "custom implementation repository" functionality, this isn't possible.

    What would be a good pattern that fulfills what I actually want to do?
    This is a main problem.
    I am asking for similar help in this post http://forum.springsource.org/showth...-response-time

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •