0

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?

2
  • what do you mean ?Do you want these tables to be dynamically when you run the project?Are you asking what you should write in the application.properties file for this? Commented Sep 5, 2021 at 13:26
  • @YusufBEŞTAŞ I dont want it to be based on every request, else I would go for multitenancy approach. I just want it to be configurable before the application starts. Commented Sep 5, 2021 at 13:29

2 Answers 2

2

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)
2
  • Thanks for the answer. What could be the next best approach if not using application.properties? Commented Sep 5, 2021 at 13:38
  • If what you want is to keep the values configured in a common and single place, you could create a constant (static final attribute) in a related file (let us say a DatabaseConfiguration class). I have edited my answer.
    – João Dias
    Commented Sep 5, 2021 at 13:46
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.