1

My persistence.xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://xmlns.jcp.org/xml/ns/persistence
        http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="primary">
        <jta-data-source>java:jboss/datasources/LMSDS</jta-data-source>
        <properties>
            <property name="hibernate.event.merge.entity_copy_observer"
                value="allow" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        </properties>
    </persistence-unit>
</persistence>

If I start my application the tables are created, because hibernate.hbm2ddl.auto is set to create. If I add new entities to my application, I don't want the existing data in my database to get lost, but I want to keep it and only create new tables.

How is that possible?

2

1 Answer 1

1

As of today, this is not possible using Hibernate versions <= 5.2.x (JPA 2.0/2.1). By design (default), you have these options available, see official documentation:

  • create - Create the associated schema from scratch.
  • update - Updates an existing schema.
  • validate - Validates if the JPA annotated model classes comply with an existing schema.
  • create-drop - Create the schema and automatically drops it when the JVM is terminated.

For further reference see an earlier question on this topic.

Maybe it's worth looking into Flyway which gives you better capabilities to manage schema migration and upgrade paths. Vlad Mihalcea has written a good blog post on this topic as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.