riorio
riorio

Reputation: 6826

Hibernate hibernate.ddl-auto - how to avoid the manual need to replace "create" with "update" after app's first launch

If I want Hibernate to create the schema for me, I need to set the property of

spring.jpa.hibernate.ddl-auto=create

And once it was created, I need to manually update it to be update or simply to comment out this property line.

If I forget to update it, every app restart will erase all DB data and re-create the schema.

I want to eliminate the manual thing here. I don't want to have to remember to change the value when the app is moving between different environments (another developer, QA env 1, QA env 2,.....)

Isn't there an option to tell hibernate: create the schema only if it is missing & update/do nothing otherwise?

Upvotes: 1

Views: 1225

Answers (1)

Jim Richards
Jim Richards

Reputation: 718

In general, this is a bad idea. create-drop and update are for testing and not recommended for production use.

Production systems should always use validate. Don't have hibernate create the tables.

You're better off using a tool like liquibase which can handle database updates for application upgrades safely. As you're using Spring, you can set up profiles in the XML that can work with maven -P setting, so that you have a profile for production like

<beans profile="liquibase">
    <bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase" depends-on="systemProperties">
        <property name="dataSource" ref="dataSource" />
        <property name="changeLog" value="classpath:liquibase-changelog.xml" />
        <property name="defaultSchema" value="${jdbc.schema}" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="liquibase">
        <property name="persistenceUnitName" value="PersistenceUnit" />
    </bean>
</beans>

Which then sets up the database on start up. Liquibase will know which statements have been run, and only run alter table statements or similar when required.

It's worth the effort.

Upvotes: 1

Related Questions