How to configure hibernate ORM on heroku?

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> 

In addition, I override 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 to set my properties so that heroku detect the postgres driver?

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

+4
source share
2 answers

The URL format that comes from Heroku Postgres is not a JDBC format. This is a polyglot format so that all platforms can use it. Therefore, you need to convert the URL to JDBC format. There is a good example of how to do this at the Heroku Dev Center:
https://devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#using-the-in-plain-jdbc

0
source

You can use this Hibernate + JPA persistence.xml configuration as the basis for the hibernation configuration.

The property names between hibernate.cfg.xml and persistence.xml same, only sleep mode uses opening and closing elements, while JPA uses attributes.

 <properties> <property name="hibernate.connection.url" value="jdbc:postgresql://ec2-107-21-126-162.compute-1.amazonaws.com:6232/dbname?username=username&amp;password=password&amp;ssl=true&amp;sslfactory=org.postgresql.ssl.NonValidatingFactory"/> <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/> <property name="hibernate.connection.username" value="username"/> <property name="hibernate.connection.password" value="password"/> <property name="hibernate.archive.autodetection" value="class"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hbm2ddl.auto" value="update"/> <!-- c3p0 connection pool settings --> <property name="hibernate.connection.provider_class" value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" /> <property name="hibernate.c3p0.min_size" value="1" /> <property name="hibernate.c3p0.max_size" value="5" /> <property name="hibernate.c3p0.acquire_increment" value="1" /> <property name="hibernate.c3p0.idle_test_period" value="3000" /> <property name="hibernate.c3p0.max_statements" value="50" /> <property name="hibernate.c3p0.timeout" value="1800" /> </properties> 

Maven Dependencies:

  <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.1.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>4.2.1.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.2.1.Final</version> </dependency> 
+1
source

All Articles