0

I need to use two different schemas in one java app. For now I'm setting the value with static text:

@Table(name = "jhi_user", catalog = "some_catalog")
@Table(name = "jhi_persistent_audit_event", catalog = "some_other_catalog")

I want to read those values from class marked with @ConfigurationProperties annotation but I don't have any ideas how to do it correctly.

I've tried to create enum with static subclass, but it only works with constant values.

public enum MyData {
    DB1(Constants.DB1),
    DB2(Constants.DB2);


    public static class Constants {
        public static final String DB1 = ApplicationProperties.getDb1(); //<--- : attribute value must be constant
        public static final String DB2 = "db2";   //<----- works 
    }
}

Is it any way to change database catalog for different classes without compiling the code? I suppose, I can set it to blank and then change them with reflection, is there a better way?

Anthon
  • 69,918
  • 32
  • 186
  • 246
  • Please see: https://stackoverflow.com/a/3880200/5334060 – the4dK Nov 09 '18 at 16:03
  • What would be an **internal** config YAML file? – Anthon Nov 09 '18 at 16:15
  • I don't think this is possible. Alternatives to investigate. [1] You can override any JPA annotation by supplying an orm.xml file (https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/xml-overriding.html). Not sure if this orm.xml could have dynamic attributes via, for example, environment vars. If not swap in at build time or externalize the orm.xml per environment??? [2] Work with mutiple spring managed datasources???. The catalog(s) can be set dynamically using Spring config at runtime. – Alan Hay Nov 09 '18 at 16:18
  • Could also externalise the orm.xml so can differ for each environment: https://vladmihalcea.com/how-to-use-external-xml-mappings-files-outside-of-jar-with-jpa-and-hibernate/ – Alan Hay Nov 09 '18 at 16:22
  • I was inaccurate - I meant project's file. To be precise - I don't want to change any setting in the runtime. I need to set them only once - when my application starts. – Miłosz Godlewski Nov 09 '18 at 17:36
  • JPA defines `orm.xml` for that very purpose ... a dumb idea is to hardcode schema info in annotations (especially when needing to change deployment). But then this is defined in any decent JPA docs –  Nov 09 '18 at 19:02

0 Answers0