I am developing a Jersey
application which is connected to a PostgreSQL
database. I am looking for a solution how to configure Hibernate
in a way that it always connects correctly to either the local or the Heroku
based database (depending on if I deploy my application locally or if I push it to Heroku
).
Using the Heroku
guides, I tried something like this:
HibernateUtil.java:
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
configuration.setProperty("hibernate.connection.url",
System.getenv("DATABASE_URL"));
URI dbUri = new URI(System.getenv("DATABASE_URL"));
String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':'
+ dbUri.getPort() + dbUri.getPath();
configuration
.setProperty("hibernate.connection.username", username);
configuration
.setProperty("hibernate.connection.password", password);
configuration.setProperty("hibernate.connection.url", dbUrl);
System.out.println("Hibernate Annotation Configuration loaded");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
System.out.println("Hibernate Annotation serviceRegistry created");
SessionFactory sessionFactory = configuration
.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
My hibernate.cfg.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 4.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection properties - Driver, URL, user, password -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/MYAPP</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<!-- org.hibernate.HibernateException: No CurrentSessionContext configured! -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- Mapping with model class containing annotations -->
<mapping class="com.example.object" />
</session-factory>
</hibernate-configuration>
The idea is that HibernateUtil overrides the hibernate.cfg.xml properties (url, user, password). Deploying locally works but deploying to Heroku fails:
remote: [ERROR] Failed to execute goal de.juplo:hibernate4-maven-plugin: .1.0:export (default) on project myapp: Execution default of goal de.juplo:hib rnate4-maven-plugin:1.1.0:export failed: Error calling Driver#connect: Connecti n to localhost:5432 refused. Check that the hostname and port are correct and t at the postmaster is accepting TCP/IP connections. Connection refused -> [Help ]