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?

MWiesner
  • 8,868
  • 11
  • 36
  • 70
antarkt1s
  • 135
  • 1
  • 2
  • 13
  • 2
    You should use `update`. Also take a look to this answer : http://stackoverflow.com/a/1689769/4536811 – Mickael May 19 '17 at 07:52
  • Write out the neccessary schema changes: Look this answer https://stackoverflow.com/questions/49775419/log-sql-from-hibernate-if-spring-jpa-hibernate-ddl-auto-update/49776600#49776600 – fkurth Apr 12 '18 at 11:21

1 Answers1

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.

MWiesner
  • 8,868
  • 11
  • 36
  • 70