Issue
Is it okay to autowire multiple repositories (JPA interface) into another repository (class) as shown below
@Repository
public class repositoryClass {
@Autowired
private Repository1 repo1;
@Autowired
private Repository2 repo2;
}
Here repository 1 and 2 are Spring Data JpaRepository interfaces
The purpose of the repositoryClass is to fetch the sql queries from the repository1 and repository2 and then execute them using hibernate, because of that I am autowiring the repo1 and repo2 into the repositoryClass
Solution
It's bad design. In addition to breaking many of the principles of SOLID this results in a very high coupling. Let's start by reviewing what repositories are made for:
The repository pattern is pretty simple. An interface defines the repository with all logical read and write operations for a specific entity (e.g User).
We can conclude from this that repositories have one and only one responsibility which is CRUD's operations on specific entities. Then, based on the SRP principle, the reason to change the repository implementation is when the mapped entity changes (e.g we added new column to our user table and we want to acquire it from database) or we want to add some new function's to perform more CRUD operations (on the mapped entity!).
Look at diagram1, if you want to change something in repository4, you have to change repository3, repository2 and repository1, so SRP principle is broken. It is an architectural disaster. This is due to the very high coupling between the different repositories. Now let's consider other design shown in diagram 2. Adapters are used to perform certain more complex operations using several repositories and to separate the application business layer from the data layer. If something changes in repository4 we will have to change SomeAdapterB however this will not affect the business logic as the adapter is a kind of gateway to the application.
In summary do not make repositories dependent on each other. Use adapters.
PS When you autowire dependencies in spring use constructor injection!
Answered By - akkolyth