JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

EclipseLink – DDL Schema Generation in JPA 2.1

March 8, 2014 by Amr Mohammed Leave a Comment

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

[code lang=”xml”]

<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>

[/code]

  • 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.

[code lang=”xml”]

<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>

[/code]

  • 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.

[code lang=”xml”]

<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>

[/code]

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.

[code lang=”xml”]

<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"/>

[/code]

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.

[code lang=”xml”]

<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"/>

[/code]

  • 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.

Filed Under: Java EE Tagged With: EclipseLink, JPA

About Amr Mohammed

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved