Issue
So i want to deploy my java app on heroku. Once deployed it sets an environment variable DATABASE_URL. I want to use that as my url for hibernate. I currently have hibernate.cfg.xml and there i set the url jdbc:postgresql://localhost:port/db like this . How can i change it to take DATABASE_URL?
Solution
One of the ways is to use setProperty(String propertyName, String value) of Configuration to explicitly override the value of hibernate.connection.url
before creating the SessionFactory.
To get the environment variables , you can use System.getenv(String name) .
/**Load the hibernate.cfg.xml from the classpath**/
Configuration cfg = new Configuration();
cfg.setProperty("hibernate.connection.url", System.getenv("DATABASE_URL"));
SessionFactory sessionFactory = cfg.buildSessionFactory();
Answered By - Ken Chan
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)