Announcement Announcement Module
Collapse
No announcement yet.
Spring Data: Calling default CRUD methods inside a custom repository implementation? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 12:55 PM.

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

    Regards.

    Comment


    • #3
      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 ).

      Comment


      • #4
        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.

        Comment


        • #5
          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?

          Comment


          • #6
            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

            Comment


            • #7
              I am running into the same issue with a circular reference when injecting the base repo into a custom implementation. Has there been any solution found to this issue?

              Thanks,

              Eric

              Comment

              Working...
              X