How can I configure Hibernate with environment variable

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?

Answers


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();

Need Your Help

Always use jquery selectors or cache them in variables?

javascript jquery performance

jQuery selectors are wonderful, but I sometimes I find myself typing them over and over, and it gets a little annoying.

How can I still get my core dump when I run something from cron which would normally core?

c++ c unix cron coredump

Today something which I cron and for which I try to detect core dumps and alert me did in fact hit a false assertion (something which normally core dumps it when I run it on the commandline either

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.