Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/Examples/JPA/DDL


EclipseLink can be used to automatically generate the tables and database schema for a persistence unit. This is done through the "eclipselink.ddl-generation" persistence unit property, set to either "create-tables" or "drop-and-create-tables". The tables and constraints will be generated for all of the classes defined in that persistence unit.

"create-tables" - will only attempt to create tables, if the table already exists then it will not be dropped or replaced, the existing table will be used.

"drop-and-create-tables" - will first drop the existing table, and then create the new table. This is useful in development when the schema is frequently changing, or for testing when the existing data needs to be cleared. Note that this will loose all of the data in the tables when they are dropped, so never use this on a production schema that has valuable data in the database. If the schema changed dramatically, there could be old constraints in the database that prevent the dropping of the old tables. This may require the old schema to be dropped through another mechanism.

By default the schema is created on the database. It is also possible to have a script generated using the "eclipselink.ddl-generation.output-mode" property. This can be set to either, "sql-script", "database", or "both". You can also define a script to be generated to create or drop the schema through "eclipselink.create-ddl-jdbc-file-name", and "eclipselink.drop-ddl-jdbc-file-name". The drop script can be used the next time the schema is replaced to drop all of the old constraints, some application servers such as Glassfish make use of this feature.

All of the EclipseLink persistence unit properties are defined in the PersistenceUnitProperties class.

Example persistence.xml

<persistence 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 persistence_2_0.xsd" version="2.0">
    <persistence-unit name="acme" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>        
        <properties>
            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
            <property name="javax.persistence.jdbc.user" value="scott"/>
            <property name="javax.persistence.jdbc.password" value="tiger"/>
            <!--property name="eclipselink.logging.level" value="FINEST"/-->
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.create-ddl-jdbc-file-name" value="createDDL_ddlGeneration.jdbc"/>
            <property name="eclipselink.drop-ddl-jdbc-file-name" value="dropDDL_ddlGeneration.jdbc"/>
            <property name="eclipselink.ddl-generation.output-mode" value="both"/>
        </properties>
    </persistence-unit>
</persistence>

Back to the top