How do I configure hibernate ORM on heroku?

Viewed 6180

Here is what I have in hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</property>
        <property name="hibernate.connection.charSet">UTF-8</property> 
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="connection.pool_size">1</property>
        <property name="show_sql">true</property>
        <property name="hibernate.use_outer_join">true</property>
        <property name="current_session_context_class">thread</property>
    </session-factory>
</hibernate-configuration>

Also, I am overriding some properties dynamically...

 Configuration config = new Configuration().configure("path_to_hibernate.cfg.xml");
 config.setProperty("hibernate.connection.url", System.getenv("HEROKU_POSTGRESQL_MYCOLOR_URL"));
 config.setProperty("hibernate.connection.username", "***");
 config.setProperty("hibernate.connection.password", "***");

But, when I run it, I get this error...

ERROR: No suitable driver found for postgres://*******:*********@ec2-23-21-85-197.compute-1.amazonaws.com:5432/d9i5vp******o7te

How do I configure my properties so that heroku finds the postgres driver?

(I am new to hibernate and heroku, so any help is greatly appreciated :)

3 Answers
        Map<String, String> jdbcUrlSettings = new HashMap<>();
        String jdbcDbUrl = System.getenv("JDBC_DATABASE_URL");
        System.out.println("jdbcDbUrl" + jdbcDbUrl);
        if (null != jdbcDbUrl) {
            jdbcUrlSettings.put("hibernate.connection.url", System.getenv("JDBC_DATABASE_URL"));
        }



        Configuration configuration = new Configuration();
        annotatedAddClass(configuration);

        StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
        serviceRegistryBuilder.applySettings(jdbcUrlSettings);
        ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
        return configuration.buildSessionFactory(serviceRegistry);
Related