1

I have seen some documentation on connecting to more than one database/data source via JPA, but this is not what I want to do exactly. I want to use one database provider for development and a completely different database provider for production (and probably QA). This question would also be applicable to supporting different production environments with different database providers (ie, DB2 for Client 1, Oracle for Client 2, MySQL for Client 3, etc).

In my case, I would like to do the following:

  • use JPA with Apache Derby for development purposes
  • use JPA with IBM DB2 for production purposes

We do not use Spring :(

With MyBatis, I could simply provide XML mappings for each database I wanted to support. And map all of results back to the same entities/POJOs.

So far it seems to me, that JPA will require custom everything up to maybe the Service layer (with entities, DAO, sql all being custom per database).

I want to know how you would go about setting up your application to use Derby for development and DB2 for production. Can I get by with using the same entities for both, should I separate the DAO layer into separate projects, can I use one persistence.xml, etc.?

Please let know what approach you would suggest to accommodate this.

jediwompa
  • 79
  • 1
  • 11
  • and you can provide XML mappings for each database in JPA too; you do NOT use annotations if you have need of using a different schema on different datastores – Neil Stockton Apr 13 '16 at 06:45

1 Answers1

0

This is a pretty common scenario, you don't need to duplicate anything if you are using JPA properly. You can use the same @Entity classes and DAOs.

If you are using Maven you can make the connection settings specific to some Maven profile, e.g. create profile for each environment and then let Maven resource filtering populate properties in your persistence.xml file (see this answer).

Community
  • 1
  • 1
rapasoft
  • 931
  • 9
  • 16