Here is what I have in hibernate.cfg.xml
The URL format that comes from Heroku Postgres is not the JDBC format. It is a polyglot format so that all platforms can use it. So you need to transform the URL to the JDBC format. There is a good example of how to do that on the Heroku Dev Center:
https://devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#using-the-in-plain-jdbc
You can use this Hibernate+JPA persistence.xml
configuration as a base for your hibernate configuration.
The property names between the hibernate.cfg.xml
and the persistence.xml
are the same, only hibernate uses opening and closing elements whereas 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&password=password&ssl=true&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 dependancies:
<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>
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);