EclipseLink – DDL Schema Generation in JPA 2.1

If you’ve a good experience in the Java Persistence API, you can configure the JPA to generates the required schema. These are not part of the JPA implementation prior to the JPA 2.1 specification. These are considered as the extensions for the JPA provider. However, this has been standardized from the JPA 2.1 release. JPA 2.1 added a series of properties for database schema maintenance and generation. This tutorial explains how to use the EclipseLink for generating schema for the database tables.

EclipseLink provides you a eclipselink.dll- generation to specify how EclipseLink generates the DDL (Data Defintion Language) for the database schema (tables and constraints) on deployment.

EclipseLink DDL Valid Values

The property of eclipselink.ddl- generation has multiple valid values and these are:

  • create-tables: EclipseLink will attempt to execute a CREATE TABLE SQL for each table. If the table already exists, EclipseLink will follow the default behavior for your specific database and JDBC driver combination (When a CREATE TABLE SQL is issued for an already existing table). In most cases an exception is thrown and the table is not created; the existing table will be used. EclipseLink will then continue with the next statement.
  • create-or-extend-tables: EclipseLink will attempt to create tables. If the table exists, EclipseLink will add any missing columns.
  • drop-and-create-tables: EclipseLink will attempt to DROP all tables, then CREATE all tables. If any issues are encountered. EclipseLink will follow the default behavior for your specific database and specific JDBC driver combination, then continue with the next statement. This is useful in development if the schema frequently changes or during testing when an existing data needs to be cleared.
  • none: default, no ddl generated; no schema generated.

Generated EclipseLink Tutorial Schema

The examples that created for serving the EclipseLink Tutorial were depends on database schema that created before. The entities that are created now contains a different relationships and properties ranging from primary keys (Simple and composite) and associations (including OneTOne, OneToMany, ManyToOne and ManyToMany) to inheritance and identities generation. But what could happen if we are going to use the EclipseLink DDL generation; is it enough for creating identical schema for that manually created schema. Let’s see.

Figure 1.0 shows you the manually created schema contains the existing associations and properties for its tables.

EclipseLink - DDL Schema Generation in JPA 2.1

Figure 1.0

  • The model described the associations from a database perspective.

Figure 1.1 shows you the schema that’s created by using the EclipseLink -ddl-generation.

JavaBeat Schema Created By Using EclipseLink - Schema Generation

Figure 1.1

  • The JavaBeat schema that shown at the Figure 1.1 displays the generated schema by using EclipseLink-ddl-generation.
  • The model describe the schema from an Object-Oriented perspective.

The Required Persistence Configuration


<properties>
 <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/JavaBeat-jpa-generate"/>
 <property name="javax.persistence.jdbc.user" value="root"/>
 <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
 <property name="javax.persistence.jdbc.password" value="root"/>
 <property name="eclipselink.logging.level" value="FINEST"/>
 <property name="eclipselink.ddl-generation" value="create-tables"/>
 </properties>

  • The property eclipselink.ddl-generation will be used to configure the strategy that would be used for create the schema.

Export the Generated DDL Into External File

You’ve a choice to export the generated schema into external DDL, you’ve to add a new property into your persistence configuration.


<properties>
 <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/JavaBeat-jpa-generate"/>
 <property name="javax.persistence.jdbc.user" value="root"/>
 <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
 <property name="javax.persistence.jdbc.password" value="root"/>
 <property name="eclipselink.logging.level" value="FINEST"/>
 <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
 <property name="eclipselink.ddl-generation.output-mode" value="sql-script"/>
 </properties>

  • The eclipselink.ddl-generation.output-mode property has three attributes that could be applied.
  1. both: DDL will be generated and written to both the database and a file.
  2. database: (Default) DDL will be generated and written to the database only.
  3. sql-script: DDL will be generated and written to a file only.
  • The ddl file will be generated and given a default name.

Assign a name to the files generated:

If you’ve desired to give a specific name for the generated files, you can use an additional eclipselink properties.


<properties>
 <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/JavaBeat-jpa-generate"/>
 <property name="javax.persistence.jdbc.user" value="root"/>
 <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
 <property name="javax.persistence.jdbc.password" value="root"/>
 <property name="eclipselink.logging.level" value="FINEST"/>
 <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
 <property name="eclipselink.ddl-generation.output-mode" value="sql-script"/>
 <property name="eclipselink.create-ddl-jdbc-file-name" value="sql-create-script"/>
// Created statements file name
 <property name="eclipselink.drop-ddl-jdbc-file-name" value="sql-drop-script"/>// Dropped statements file name
 </properties>

Change the location of the created files:

If you’ve desired to change the location of the generated files, you can use an additional eclipselink property called eclipselink.application-location.


<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/JavaBeat-jpa-generate"/>
 <property name="javax.persistence.jdbc.user" value="root"/>
 <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
 <property name="javax.persistence.jdbc.password" value="root"/>
 <property name="eclipselink.logging.level" value="FINEST"/>
 <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
 <property name="eclipselink.ddl-generation.output-mode" value="sql-script"/>
 <property name="eclipselink.create-ddl-jdbc-file-name" value="sql-create-script"/>
 <property name="eclipselink.drop-ddl-jdbc-file-name" value="sql-drop-script"/>
 <property name="eclipselink.application-location" value="c:/javabeat-database-files"/>

Figure 1.2 shows you the files created under the specified folder.

Created Files Under Specific Folder

Figure 1.3

Deploy-On-Startup

If you’ve desired to generate the DDL schema upon that’s time the entity manager factory acquired not by creation of entity manager instance you have to provide a eclipselink deploy-on-startup property. That property accepting two values true or false, where the true means the DDL generation should started once the factory of entity manager instance has been created, meanwhile the false means the DDL generation should started once the entity manager instance has been created.


<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/JavaBeat-jpa-generate"/>
 <property name="javax.persistence.jdbc.user" value="root"/>
 <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
 <property name="javax.persistence.jdbc.password" value="root"/>
 <property name="eclipselink.logging.level" value="FINEST"/>
 <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
 <property name="eclipselink.ddl-generation.output-mode" value="sql-script"/>
 <property name="eclipselink.create-ddl-jdbc-file-name" value="sql-create-script"/>
 <property name="eclipselink.drop-ddl-jdbc-file-name" value="sql-drop-script"/>
 <property name="eclipselink.application-location" value="c:/javabeat-database-files"/>
 <property name="eclipselink.deploy-on-startup" value="false"/>

  • This property is important if you’ve worked in a huge application, in that the database creation might take a while or in case the factory creation take place at the initialization of the application and the entity manager not created yet.

Summary

Eclipselink provides much extension properties that can be stated at the persistence.xml file which can be used to configure the EclipseLink JPA framework. This tutorial mentioned properties that can be used to generate a database schema based on the created objects.

This is not only the properties that could be provided by the eclipse for such that configuration, but also it contains other additional properties might be discussed on later tutorial for handling eclipselink exception, for accessing eclipselink descriptor and mapping API.

Comments

comments

About Amr Mohammed

Speak Your Mind

*