26

I've been briefly looking at JPA recently, and I was wondering what the deal is with database schema migrations and staying lined up with the classes you've created.

Is there support in JPA for this stuff? Utilities? Best Practises?

Cheers!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
brabster
  • 42,504
  • 27
  • 146
  • 186
  • 2
    I read the book about JPA recently. The JPA 2.1 was released on 2013, does it support migrations now ? – AechoLiu Dec 11 '14 at 03:14

3 Answers3

12

I won't rely on JPA providers to update the database schema. Check Liquibase for one of the good approaches.

lexicore
  • 42,748
  • 17
  • 132
  • 221
7

The short answer is no.

If you change your beans, then you will have to migrate the existing schema by hand. So for Rails style database migrations you will have to look elsewhere.

You can however generate the initial ddl from your Java beans easily. The example below illustrates schema creation with EclipseLink version 2.0:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="JPATestPU" transaction-type="RESOURCE_LOCAL">
        <provider>
            org.eclipse.persistence.jpa.PersistenceProvider
        </provider>
        <class>org.randompage.MyEntity</class>
        <properties>
            <property name="javax.persistence.jdbc.user" value="johndoe"/>
            <property name="javax.persistence.jdbc.password" value="secret"/>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:~/.h2/testdb;FILE_LOCK=NO"/>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.logging.level" value="INFO"/>
        </properties>
    </persistence-unit>
</persistence>

The key element here is

 <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> 

This tells EclipseLink to drop existing tables and generate new once from your JPA mapping. This procedure is highly vendor specific so for other JPA vendors (Hibernate, OpenJPA...) you will have to consult their specific documentation.

Lars Tackmann
  • 20,275
  • 13
  • 66
  • 83
  • Writing the persistence.xml unit file by yourself is relatively hard (it is documented openly and is very "generic" (=vendor-independent) format but the values are vendor-specific). However, most modern IDEs like NetBeans provide a way to manage it for you. Sure you can write it on your own but a lot boggy-traps and pitfalls wait for you but teaches you (surely) a lot. – Roland Jun 20 '18 at 08:34
  • This is only correct for when you initialize (freshly create) your database scheme. So you may have to write your own migration tool which could be a bit tricky. Maybe something like keep old entities and updated entities in one project and separate databases. Then connect to both databases and load with old entities from old database and write (persist) with new entities to new database? – Roland Jun 20 '18 at 08:38
0

If you set the generateDdl to Hibernate (if it is the underlying implementation), then it generates the database schema according to your current dialect. So after changing the dialect it will automatically generate the database.

Other JPA providers may have different properties for this.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 2
    This is correct, but it doesn't address the issue of schema migration. – Dave Jul 09 '10 at 02:22
  • @HDave - on the contrary. The schema is replicated to the new database, reflecting the current data model. – Bozho Jul 09 '10 at 05:18
  • 7
    By schema migration, I believe the OP means having scripts that can take an existing database with data and bring it up to date with the new entities/schema. Often this involves careful study of the schema changes and sometime the unload/reloading of data so that FK's can be changed, PK's can be split, etc. Hibernate can generate a script to make a new schema. Hibernate can also generate a script to modify an old schema to make it a new schema, but it assumes the database has no data -- and even then my understanding is that capability is seldom used because of "issues." – Dave Jul 09 '10 at 15:02