Want to know how to implement best practices for accessing split tables in Spring Data JPA

Asked 2 years ago, Updated 2 years ago, 153 views

For example, a user table with user information
user_0, user_1, user_2, user_3 (when userId is divided by 4, the remainder follows the table name)
I would like to know how to implement without having to implement multiple entities and repositories for as many tables as possible.

In the worst case scenario, I am trying to dynamically change the repository used in the service layer, but I do not want to implement multiple similar entities and repositories.

Thank you for your cooperation.

mysql spring spring-boot database jpa

2022-09-30 20:19

1 Answers

As for the approach from DB, I think there is a way to prepare a UNION VIEW if the table has the same structure.In other words, if you create a VIEW user like the following, one entity and one repository will be enough.

CREATE VIEW user as(
    select * from user_0
    union
    select * from user_1
    union
    select* from user_2
    union
    select * from user_3
    );


2022-09-30 20:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.