Our database at the moment have 2 schema, one for production, one for testing, identical table but different data.
For server side, we use RoutingDatasource to manage the data. Each request have a parameter to know which database to access, like "prod" or "test". The parameter then store in ThreadLocal. Our RoutingDatasource will get this parmeter out, depend on the value and return the correct datasource. So we only need one mapping entity and one CrudRepository for same table in both schema.
But for some (stupid) reason, we were asked to merge both schema into one schema, having a prefix in front of each table, like PROD_TABLE1, TEST_TABLE1, PROD_TABLE2, TEST_TABLE2, etc.
So i want to ask, how can we keep only 1 entity, 1 repository for each table with this requirement? We don't want to clone code, create too many class inherit the existing one just to map to the second table. @SecondaryTable seem not what we looking for.
===============================
UPDATE 1:
I'm looking at PhysicalNamingStrategy, able to add a prefix to table name. But this only run once when they init the EntityManager. So is there any other way to add a prefix to table on each request?
===============================
UPDATE 2: I try with something like this:
@Table(name = "##schema##TABLE1")
Then add a Hibernate interceptor to our project. When prepare statement, we get the parameter from ThreadLocal, then replace the "##schema##" part with correct prefix. Using this way, everything work, but i don't really like this solution.
Anyone have any better way to do it?