1

Here are the details:

I have multiple Models in my application, each model has a repository that extends Spring's CrudRepository.

For example:

  • EyeColorModel has repository interface EyeColorRepository extends CrudRepository<EyeColor, Long>

  • HairColorModel has repository interface HairColorRepository extends CrudRepository<HairColor, Long>

  • StateModel has repository interface StateRepository extends CrudRepository<State, Long>

  • and so on ...

I would like to create a generic MyApplicationRepository that extends all of my individual repositories so that I need only create a single instance of MyApplicationRepository instead of creating multiple instances of my individual repositories.

I tried this:

public interface MyApplicationRepositoryInterface extends
    EyeColorRepository,
    HairColorRepository,
    StateRepository {
}


public class MyApplicationRepository implements MyApplicationRepositoryInterface {
}

However, when I try to extend all of my individual repositories in MyApplicationRepositoryInterface I get this error:

CrudRepository cannot be inherited with different arguments: <com.myapp.Models.EyeColor, java.lang.Long> and <com.myapp.Models.HairColor, java.lang.Long>

So is there a way to do what I would like to do, or am I stuck with instantiating instances of all my model repositories?

Brian
  • 1,726
  • 2
  • 24
  • 62
  • Could you please answer a couple of questions to understand your approach - What is it that you are trying to achieve with a generic Repository - what problem does having a single instance solve for you? – priyank-sriv Apr 25 '19 at 15:09
  • Not really a problem at all. I would just prefer to instantiate an instance of a single repository rather than instances of multiple repositories. Overall it would create less work for me and make for cleaner looking code ... IMHO. – Brian Apr 25 '19 at 15:12

3 Answers3

2

First, I'll try to explain the error you're getting: CrudRepository is a parameterised interface and inheriting from (multiple) CrudRepository interfaces of different parameterised-types is creating a conflict at runtime.

IMO, what you're trying to do is counter-productive. Spring's data repository provides a type-safe implementation of CRUD methods at runtime, when you extend CrudRepository. This gives you cleaner code, and provides compile-time correctness. And, CrudRepository is already pretty generic - it's better that way (at the interface level, than in implementation).

Even if you were to create such a repository, you would have to give up the use of CrudRepository and create a single class with all the CRUD methods across all your models (eg. saveEyeColor, saveHairColor, etc). Maybe using something like a SpringTemplate. This isn't the best way to go about it IMO, as you'll be mixing your domain objects and the class will become a nightmare to maintain.

To answer your question, yes you'll have to inject an individual repository instance for each model.

priyank-sriv
  • 1,094
  • 6
  • 12
  • Ok, I understand what you are saying. Thank you. So, am I going about it correctly with a Model and a Repository for each (e.g., eye color, hair color, state)? What do you mean by "inject an individual repository"? I am not super Hibernate savvy, I have heard of dependency injection. Is this what I need to do? – Brian Apr 25 '19 at 15:37
  • I'm talking about Spring's dependency-injection. So, you have a repository like this: `@Repository interface HairColorRepository extends CrudRepository`. Now you can inject the same into your service class, using `@Autowired HairColorRepository repository;`. Just to give a brief idea. – priyank-sriv Apr 25 '19 at 15:39
0

This is wrong approach. CrudRepositories are not supposed to be implemented. You just create Repository interfaces for all the models by extending the CrudRepository Interface. Spring provides the implementation at Runtime.

If you want to access these via a single class, then create a Service class with methods that access the required methods in these individual Repositories.

Garima
  • 121
  • 4
0

If you are looking for running a custom SQL query in spring boot with repository structures. Please have a look.

https://stackoverflow.com/a/71501509/4735043

Jin Lim
  • 1,759
  • 20
  • 24