Configuring the JPA 2.1 schema generator

In the jpa-2.0 feature, which is built on OpenJPA, you can generate data definition langugage (DDL) or interact directly with the database to define table schemas based on the JPA entity definition by using the SchemaMapper tool. In the jpa-2.1 feature, which is built on EclipseLink, you can use the new Schema Generator feature added to the JPA 2.1 specification, which has similar functions to the OpenJPA SchemaMapper.

About this task

If you need functions similar to the OpenJPA SchemaMapper, you can configure the Schema Generator feature that is in the JPA 2.1 specification.

Procedure

  1. In the persistence unit definition, within the persistence.xml file, specify the database action property with the possible values of: none, create, drop, drop-and-create.
    Each value corresponds to the action that is taken against the database. The following example causes the tables that correspond to the entities specified in the persistence unit to be dropped and new tables are created in their place.
    <persistence-unit name="pu">
            <properties>
                <property name="javax.persistence.schema-generation.database.action"
            value="drop-and-create" />
    ...
            </properties>
    </persistence-unit>
  2. Specify the script action property with the possible values of: none, create, drop, drop-and-create.
    If any value other than none is specified, you must specify a target property as well. This means that if the script action is create, which generates the create statements for the entity definition, you must specify a corresponding create target property with a target file where the statements are written to.
    <persistence-unit name="pu">
            <properties>
                <property name="javax.persistence.schema-generation.scripts.action"
          value="drop-and-create" />
                <property name="javax.persistence.schema-generation.scripts.create-target"
            value="createTargetFile.ddl"/>
                <property name="javax.persistence.schema-generation.scripts.drop-target"
          value="sampleDrop.ddl"/>
    ...
            </properties>
    </persistence-unit>