Home > Back-end >  Different schema for different entites
Different schema for different entites

Time:09-17

I have 2 entities in my spring boot project

@Entity
@Table(name = "TABLE1", schema="Sch1")
...

@Entity
@Table(name = "TABLE2", schema="Sch2")

I want both these schemas to be configurable in application.properties. Is there a way to do that?

CodePudding user response:

This can't be done.

In Java, an annotation attribute value must be a constant expression because it is resolved at compile time. Hence, having this configured dynamically based on application.properties is not possible.


Assuming that having these values configured in a common and single place is ok and enough for your use case you could do the following:

public class DatabaseConfiguration {
    static final String TABLE1_SCHEMA = "Sch1";
    static final String TABLE2_SCHEMA = "Sch2";

    // (...)
}

@Entity
@Table(name = "TABLE1", schema = DatabaseConfiguration.TABLE1_SCHEMA)
...

@Entity
@Table(name = "TABLE2", schema = DatabaseConfiguration.TABLE2_SCHEMA)

CodePudding user response:

No you can, but what you can try to do, is to define the entity metadata in the xml file: https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/xml-overriding.html

Then you can combine this xml file and maven resource filtering: https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

Or if you use gradle, then you should call processResources gradle task.

To define this schema info at build time.

  • Related