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

Difference between revisions of "EclipseLink/Examples/JPA/DDL"

 
(3 intermediate revisions by one other user not shown)
Line 5: Line 5:
 
<code>"create-tables"</code> - 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.
 
<code>"create-tables"</code> - 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.
  
<code>"drop-and-create-tables"</code> - 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.
+
<code>"drop-and-create-tables"</code> - 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 lose 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 <code>"eclipselink.ddl-generation.output-mode"</code> property.  This can be set to either, <code>"sql-script"</code>, <code>"database"</code>, or <code>"both"</code>.  You can also define a script to be generated to create or drop the schema through <code>"eclipselink.create-ddl-jdbc-file-name"</code>, and <code>"eclipselink.drop-ddl-jdbc-file-name"</code>.  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.
 
By default the schema is created on the database.  It is also possible to have a script generated using the <code>"eclipselink.ddl-generation.output-mode"</code> property.  This can be set to either, <code>"sql-script"</code>, <code>"database"</code>, or <code>"both"</code>.  You can also define a script to be generated to create or drop the schema through <code>"eclipselink.create-ddl-jdbc-file-name"</code>, and <code>"eclipselink.drop-ddl-jdbc-file-name"</code>.  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 [http://www.eclipse.org/eclipselink/api/2.1/org/eclipse/persistence/config/PersistenceUnitProperties.html PersistenceUnitProperties] class.
 
All of the EclipseLink persistence unit properties are defined in the [http://www.eclipse.org/eclipselink/api/2.1/org/eclipse/persistence/config/PersistenceUnitProperties.html PersistenceUnitProperties] class.
 +
 +
The tables and columns will be defaulted based on the mappings.  If a <code>@Table</code> or <code>@Column</code> was not provided for a class or attribute then it will be defaulted using the JPA conventions.  EclipseLink will default the table to the class or alias name as uppercase, and the column names to the attribute name as uppercase.  EclipseLink can normally infer the attribute type from the Java type, but Java does not define a size, so the size will be defaulted to 255 for a VARCHAR.  The column size and type info can be set through the <code>@Column</code> annotation.
 +
 +
The DDL generate will be specific to the database platform.  EclipseLink will infer the database platform from the JDBC driver, but it can also be set explictly using the <code>"eclipselink.target-database"</code> property.
 +
 +
DDL can also be generated through the EclipseLink API using the [http://www.eclipse.org/eclipselink/api/2.1/org/eclipse/persistence/tools/schemaframework/package-frame.html schema framework] or the [http://www.eclipse.org/eclipselink/api/2.1/org/eclipse/persistence/tools/schemaframework/DefaultTableGenerator.html DefaultTableGenerator].
  
 
== Example persistence.xml==
 
== Example persistence.xml==

Latest revision as of 11:56, 9 July 2012


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

The tables and columns will be defaulted based on the mappings. If a @Table or @Column was not provided for a class or attribute then it will be defaulted using the JPA conventions. EclipseLink will default the table to the class or alias name as uppercase, and the column names to the attribute name as uppercase. EclipseLink can normally infer the attribute type from the Java type, but Java does not define a size, so the size will be defaulted to 255 for a VARCHAR. The column size and type info can be set through the @Column annotation.

The DDL generate will be specific to the database platform. EclipseLink will infer the database platform from the JDBC driver, but it can also be set explictly using the "eclipselink.target-database" property.

DDL can also be generated through the EclipseLink API using the schema framework or the DefaultTableGenerator.

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