In my Java EE application, I always configure my persistence.xml
file with this option :
<property name="hibernate.hbm2ddl.auto" value="create-drop">
It's really handy for tests. When I commit my code, persistence.xml is committed with this configuration. However, when I deploy to production, I don't want to drop my database.
How to overcome this problem?
You should never use the hibernate.hbm2ddl.auto
for an enterprise application. It's better if you use Flyway
for both production and integration tests.
That being said, even using an in-memory DB like H2 is not desirable. You can run integration tests on MySQL and PostgreSQL almost as fast as H2 if you map the data folder on tmpfs
.
So, back to your problem. If you really need to recreate tables only on test env but not on production, then you need to use a Maven parameter for the hibernate.hbm2ddl.auto
property:
<property name="hibernate.hbm2ddl.auto" value="${hbm2ddl.value}">
For the production profile, the value is:
<hbm2ddl.value>none</hbm2ddl.value>
While for the development profile (default), the value is:
<hbm2ddl.value>create-drop</hbm2ddl.value>