Hibernate: override data from persistence.xml
If you want to override data you defined in your java-application's jpa/hibernate persistence.xml file you can pass a Map<String,Object> to the persistence-api when creating the entityManagerFactory (in this case using an environment-variable to set the connection url)
Map<String, Object> configOverrides = new HashMap<String, Object>();
configOverrides.put("hibernate.hbm2ddl.auto", "create-drop");
// override: <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mydb?autoReconnect=true"/>
String overrideDbHost = System.getenv("FAW_DB_HOST_STRING");
if (overrideDbHost!=null) {
System.out.println("Override DB_HOST to "+overrideDbHost);
configOverrides.put("hibernate.connection.url", "jdbc:mysql://"+overrideDbHost+"/mydb?autoReconnect=true");
}
emf = Persistence.createEntityManagerFactory("mydbconf", configOverrides);
Read more: