0

I am using spring data JDBC.

I normally use the specific repository object as auto-wired and perform the necessary operation.

Let us consider, I have employees who belong to a specific continent.

We have a repository for each continent.

After retrieving data from employee table, based on his continent value, need to initialize the specific continent reporisotory.

Can you please guide me on how to create a repository object dynamically?

user1862354
  • 117
  • 11
  • Or you could create a service with all the repos wired in and use the employee info to decide which to call. Don’t see why creating a repository dynamically is required. – Nathan Hughes Apr 20 '21 at 21:03
  • Thanks. But the actual use case is the main table contains the name of the child table as one of the attributes. Currently, we have 400+ child tables. It is not a viable option to create 400+ repsotiries in the service class. – user1862354 Apr 20 '21 at 21:14
  • Is there a way to create SimpleJdbcRepository? – user1862354 Apr 20 '21 at 21:15
  • Single Responsibility is always recommended. It might be a little extra effort for now but it pays you back really well. – Ajay Kumar Apr 20 '21 at 21:31
  • I think https://stackoverflow.com/a/19443031/66686 applies (it is really the same for Spring Data JDBC and JPA) – Jens Schauder May 03 '21 at 14:24

1 Answers1

0

To at least hint to an answer for the verbatim question: Take a look at JdbcRepositoryFactoryBean and JdbcRepositoryFactoryBeanUnitTest.

But I don't think this would solve your problem nor would anything in that direction.

What you might find helpful though is JdbcAggregateTemplate. It is hard to tell since you share very little about what you actually want to achieve.

In general Spring Data is not build for data models that are self referencing, i.e. where you obtain table or column names from the data model itself at runtime. In such cases it is probably better to create your own repository implementation, which takes your special use case into account but is limited to the exact features you need which very often is sufficiently limited to be maintainable.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348