P
P
Pavel Talaiko2020-10-12 17:30:24
Java
Pavel Talaiko, 2020-10-12 17:30:24

How to change database schema in Entity Hibernate?

Hi, friend. I want to understand how to dynamically change the schema in entities.

In dev environment DEV.EntityValue in prod PROD.EntityValue.
Wouldn't want to specify @Table(name = "VALUES", schema = "DEV").

@Entity
@Table(name = "VALUES")
public class EntityValue extends BaseEntity  {}


How to solve this problem?
Yes, and in some stands you need to use two schemes for different entities at once. Help solve the problem!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Roo, 2020-10-12
@xez

Stop, friend! You are clearly not going there!
The code should not depend on the environment at all. It's too much of a connection.
Prod, test, qa, staging, dev (etc..) should be separated at the configuration level.

O
Orkhan, 2020-10-12
Hasanly @azerphoenix

Hello!
If I understood correctly, then you have a base in which there are 2 schemes. And depending on the profile you want to switch schemes?
I see a couple of options here:
1) at the gradle level || maven define schemas.
https://stackoverflow.com/questions/47240702/jpa-e...
https://stackoverflow.com/questions/1149352/using-...

<profiles>
    <profile>
      <id>production</id>
      <properties>
        <schema.name>production_schema_name</schema.name>
      </properties>
    </profile>
    <profile>
      <id>test</id>
      <properties>
        <schema.name>test_schema_name</schema.name>
      </properties>
    </profile>
</profiles>

2) use different databases for different profiles. Those. create 2 databases, one for each profile.
3) Create 2 packages (dev, prod) with entities, where for each of the entities specify:
For development
@Table(name = "VALUES", schema = "DEV").
entities For production entities
@Table(name = "VALUES", schema = "PROD").
Well, use the annotation@Profile

M
mystifier, 2020-10-12
@mystifier

As an option, you can create an Interceptor - the successor of org.hibernate.EmptyInterceptor and in the onPrepareStatement method replace at least the scheme, at least the table name. To do this, you need to have information about the build profile (dev / prod).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question