Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject Repository into RepositoryCustom #2384

Closed
niroussel opened this issue Dec 9, 2021 · 4 comments
Closed

Inject Repository into RepositoryCustom #2384

niroussel opened this issue Dec 9, 2021 · 4 comments
Labels
for: stackoverflow A question that's better suited to stackoverflow.com

Comments

@niroussel
Copy link

Until SpringBoot 2.4.1, it was possible to inject a repository into its custom implementation.

This was really useful to avoid having several repositories for the same Entity.

For example, this doesn't work anymore:

  • <package 'data'>
    • UserRepository.java
    • <packages 'data.custom'>
      • UserRepositoryCustom.java
      • UserRepositoryImpl.java

With :

public class UserRepositoryImpl implements UserRepositoryCustom {

    @Autowired
    private UserRepository repo;

    public List<User> veryComplicatedSearch() {
        ....
        ....
        return repo.findAll(veryComplicatedPredicate);
    }
}

This will lead to this exception:

      ↓
┌─────┐
|  userRepositoryImpl (field private data.UserRepository data.custom.UserRepositoryImpl.repo)
└─────┘


Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 9, 2021
@mp911de
Copy link
Member

mp911de commented Dec 10, 2021

In short, this isn't possible as repository fragments are required to create the repository instance. You can either work around it by using @Lazy or ObjectProvider (or look up the repository instance at the time of using it via ApplicationContext).

@mp911de mp911de closed this as completed Dec 10, 2021
@mp911de mp911de added for: stackoverflow A question that's better suited to stackoverflow.com and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 10, 2021
@RadouaneRoufid
Copy link

What's the correct approach to address this problem without using the workarounds above ?

What if the fragment needs the repository as mentioned by @niroussel ?

public class UserRepositoryImpl implements UserRepositoryCustom {

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<User> searchByCriteria(UserSearchCriteria searchCriteria) {
        return userRepository.findAll(UserSpecifications.build(searchCriteria));
    }
}

@s-volkov-1
Copy link

What's the correct approach to address this problem without using the workarounds above ?

Maybe one of:

  • using default methods inside UserRepository interface
  • creating UserService/UserSearchService, which hosts additional logic for working with repository

@samuelpsfung
Copy link

samuelpsfung commented Nov 9, 2023

I've this workaround

@Repository
public class UserRepositoryImpl implements UserRepositoryCustom {

    @PersistenceContext
    private EntityManager entityManager;

    private JpaSpecificationExecutor<User> executor;

    @PostConstruct
    public void init() {
        executor = new SimpleJpaRepository<User, String>(User.class, entityManager);
    }

    @Override
    public List<User> searchByCriteria(UserSearchCriteria searchCriteria) {
        return executor.findAll(UserSpecifications.build(searchCriteria));
    }
}

creating UserService/UserSearchService, which hosts additional logic for working with repository

i think the query logic should be kept in the repository layer; should not be leaked to the service layer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com
Projects
None yet
Development

No branches or pull requests

6 participants