'How to connect same entity with different tables (Spring Boot)

I'm doing some research around cryptocurrencies and when getting to the technical aspects of it I found a problem that maybe its more common and someone already found a solution.

I have found a database with historic information by product and it has different tables for the different combinations but the structure of the table is the same.

I have design this DBO, nothing rocket science:

public class ProductHistoryDbo {
    private long id;
    private long startTime;
    private long endTime;
    private float low;
    private float high;
    private float open;
    private float close;
    private float volume;
}

And the database has one table per (exchange, currency_in, currency_to)

product_history_gdax_bch_btc
product_history_gdax_bch_eur
...

There are 12 tables with the same structure and one additional with all the other tables that you can find inside.

So my idea is to have only one Entity and Repository but dynamically change, if possible, from which table to retrieve the data in spring-boot in order to adapt if in the future new tables are added without the need of adding boilerplate code.

Final E2E is to have an admin page with a combobox with all the tuples which will do a request to this server and changes in the database will not imply a change in the backend code.



Solution 1:[1]

You can create a base class and then extend it with the only difference for each final class being the table name.

Base class:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Lut implements BaseCrudEntity<Long> {

}

Subclasses:

package xxx.lut;
import javax.persistence.*;

@Entity
@Table(name = "rwx_gnrl_lut_dm")
public class LutDm extends Lut {

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Syscall