Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to different databases (development/test/production) transparently with hibernate

I have several different databases for different environments to connect my application to. These are constant per installation but differ between them. In other words there is

  1. A development environment which connects to a development database
  2. A test environment which connects to a test database and finally
  3. The production environment with its own database

Hibernate is used via Annotations and the code doesn't 'know' which environment it's run in. All the databases are mySQL driven but differ in url, username and password.

Currently I removed the hibernate.cfg.xml from the application and moved it to the home directory of the application-server-user but this doesn't seem like a very good solution for security reasons and because this introduced the need to manually update any changed mappings when updating an environment.

I really hope, there is a better solution but I can't find one.

like image 900
Nicktar Avatar asked Oct 12 '11 10:10

Nicktar


1 Answers

You can bundle the hibernate.cfg.xml in a .jar or you could use a JPA (persistence.xml) approach instead which allows you to have different "persistence units" that you can then choose based on any variable you'd like (a properties file in your home for example). See http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/configuration.html#setup-configuration-packaging for an example of a persistence.xml.

like image 58
Thor84no Avatar answered Oct 12 '22 10:10

Thor84no