[jboss-jira] [JBoss JIRA] (WFLY-1186) Create JPA service callback for invoking database migration

Scott Marlow (JIRA) jira-events at lists.jboss.org
Fri May 24 22:13:06 EDT 2013


    [ https://issues.jboss.org/browse/WFLY-1186?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12776996#comment-12776996 ] 

Scott Marlow commented on WFLY-1186:
------------------------------------

>From JPA 2.1 section 9.4 Schema Generation
{quote}
In cases where a preconfigured database (or a “legacy” database) is not used or is not available, the Java Persistence schema generation facility may be used to generate the tables and other database artifacts required by the persistence application. Whether schema generation entails the creation of schemas proper in the database is determined by the environment and the configuration of the schema generation process, as described below.  Schema generation may happen either prior to application deployment or when the entity manager factory is created as part of the application deployment and initialization process.

* In Java EE environments, the container may call the PersistenceProvider generateSchema method separately from and/or prior to the creation of the entity manager factory for the persistence unit, or the container may pass additional information to the createContainerEntityManagerFactory call to cause schema generation to happen as part of the entity manager factory creation and application initialization process. The information passed to these methods controls whether the generation occurs directly in the target database, whether DDL scripts for schema generation are created, or both.

* In Java SE environments, the application may call the Persistence generateSchema method separately from and/or prior to the creation of the entity manager factory or may pass information to the createEntityManagerFactory method to cause schema generation to occur as part of the entity manager factory creation.

The application may provide DDL scripts to be used for schema generation as described in Section 8.2.1.9. The application developer may package these scripts as part of the persistence unit or may specify strings corresponding to file URLs for the location of such scripts. In Java EE environments, such scripts may be executed by the container, or the container may direct the persistence provider to execute the scripts. In Java SE environments, the execution of the scripts is the responsibility of the persistence provider. In the absence of the specification of scripts, schema generation, if requested, will be determined by the object/relational metadata of the persistence unit.

The following standard properties are defined for configuring the schema generation process. In Java EE environments these properties are passed by the container in the Map argument to either the PersistenceProvider generateSchema method or the createContainerEntityManagerFactory method. In Java SE environments, they are passed in the Map argument to either the Persistence generateSchema method or createEntityManagerFactory method.  In Java EE environments, any strings corresponding to file URLs for script sources or targets must spec-
ify absolute paths (not relative). In Java EE environments, all source and target file locations must be
accessible to the application server deploying the persistence unit

*  javax.persistence.schema-generation.database.action
The javax.persistence.schema-generation.database.action property specifies the action to be taken by the persistence provider with regard to the database artifacts.  The values for this property are "none","create", "drop-and-create", "drop". If the javax.persistence.schema-generation.database.action property is not specified, no schema generation actions must be taken on the database.

*  javax.persistence.schema-generation.scripts.action
The javax.persistence.schema-generation.scripts.action property specifies which scripts are to be generated by the persistence provider. The values for this property are "none", "create", "drop-and-create", "drop". A script will only be generated if the script target is specified. If this property is not specified, no scripts will be generated.

*   javax.persistence.schema-generation.create-source
The javax.persistence.schema-generation.create-source property specifies whether the creation of database artifacts is to occur on the basis of the object/relational mapping metadata, DDL script, or a combination of the two. The values for this property are "metadata", "script", "metadata-then-script", "script-then-metadata". If this property is not specified, and a script is specified by the javax.persistence.schema-generation.create-script-source property, the script (only) will be used for schema generation; otherwise if this property is not specified, schema generation will occur on the basis of the object/relational mapping metadata (only). The "metadata-then-script" and "script-then-metadata" values specify that a combination of metadata and script is to be used and the order in which this use is to occur. If either of these values is specified and the resulting database actions are not disjoint, the results are undefined and schema generation may fail.

*  javax.persistence.schema-generation.drop-source
The javax.persistence.schema-generation.drop-source property specifies whether the dropping of database artifacts is to occur on the basis of the object/relational mapping metadata, DDL script, or a combination of the two. The values for this property are "metadata", "script", "metadata-then-script", "script-then-metadata". If this property is not specified, and a script is specified by the javax.persistence.schema-generation.drop-script-source property, the script (only) will be used for the dropping of database artifacts; otherwise if this property is not specified, the dropping of database artifacts will occur on the basis of the object/relational mapping metadata (only). The "metadata-then-script" and "script-then-metadata" values specify that a combination of metadata and script is to be used and the order in which this use is to occur. If either of these values is specified and the resulting database actions are not disjoint, the results are undefined and the dropping of database artifacts may fail.

*  javax.persistence.schema-generation.create-database-schemas
In Java EE environments, it is anticipated that the Java EE platform provider may wish to control the creation of database schemas rather than delegate this task to the persistence provider.  The javax.persistence.schema-generation.create-database-schemas property specifies whether the persistence provider is to create the database schema(s) in addition to creating database objects such as tables, sequences, constraints, etc. The value of this boolean property should be set to true if the persistence provider is to create schemas in the database or to generate DDL that contains “CREATE SCHEMA” commands. If this property is not supplied, the provider should not attempt to create database schemas. This property may also be specified in Java SE environments.

*  javax.persistence.schema-generation.scripts.create-target, javax.persistence.schema-generation.scripts.drop-target
If scripts are to be generated, the target locations for the writing of these scripts must be specified.
The javax.persistence.schema-generation.scripts.create-target property specifies a java.IO.Writer configured for use by the persistence provider for output of the DDL script or a string specifying the file URL for the DDL script. This property should only be specified if scripts are to be generated.
The javax.persistence.schema-generation.scripts.drop-target property specifies a java.IO.Writer configured for use by the persistence provider for output of the DDL script or a string specifying the file URL for the DDL script. This property should only be specified if scripts are to be generated.

*  javax.persistence.database-product-name, javax.persistence.database-major-version, javax.persistence.database-minor-version
If scripts are to be generated by the persistence provider and a connection to the target database is not supplied, the javax.persistence.database-product-name property must be specified. The value of this property should be the value returned for the target database by the JDBC DatabaseMetaData method getDatabaseProductName. If sufficient database version information is not included in the result of this method, the javax.persistence.database-major-version and javax.persistence.database-minor-version properties should be specified as needed. These should contain the values returned by the JDBC getDatabaseMajorVersion and getDatabaseMinor-Version methods respectively.

*  javax.persistence.schema-generation.create-script-source, javax.persistence.schema-generation.drop-script-source
The javax.persistence.schema-generation.create-script-source and javax.persistence.schema-generation.drop-script-source properties are used for script execution. In Java EE container environments, it is generally expected that the container will be responsible for executing DDL scripts, although the container is permitted to delegate this task to the persistence provider. If DDL scripts are to be used in Java SE environments or if the Java EE container delegates the execution of scripts to the persistence provider, these properties must be specified.  The javax.persistence.schema-generation.create-script-source prop-
erty specifies a java.IO.Reader configured for reading of the DDL script or a string designating a file URL for the DDL script.

The javax.persistence.schema-generation.drop-script-source property specifies a java.IO.Reader configured for reading of the DDL script or a string designating a file URL for the DDL script.

*  javax.persistence.schema-generation.connection
The javax.persistence.schema-generation.connection property specifies the JDBC connection to be used for schema generation. This is intended for use in Java EE environments, where the platform provider may want to control the database privileges that are available to the persistence provider. This connection is provided by the container, and should be closed by the container when the schema generation request or entity manager factory creation completes. The connection provided must have credentials sufficient for the persistence provider to carry out the requested actions. If this property is not specified, the persistence provider should use the DataSource that has otherwise been provided.

{quote}

JPA spec 2.1 section 9.4.1 Data Loading
{quote}
Data loading, by means of the use of SQL scripts, may occur as part of the schema generation process after the creation of the database artifacts or independently of schema generation. The specification of the javax.persistence.sql-load-script-source controls whether data loading will occur.

*  javax.persistence.sql-load-script-source
In Java EE container environments, it is generally expected that the container will be responsible for executing data load scripts, although the container is permitted to delegate this task to the persistence provider. If a load script is to be used in Java SE environments or if the Java EE container delegates the execution of the load script to the persistence provider, this property must be specified.

The javax.persistence.sql-load-script-source property specifies a java.IO.Reader configured for reading of the SQL load script for database initialization or a string designating a file URL for the script.
{quote}
                
> Create JPA service callback for invoking database migration
> -----------------------------------------------------------
>
>                 Key: WFLY-1186
>                 URL: https://issues.jboss.org/browse/WFLY-1186
>             Project: WildFly
>          Issue Type: Feature Request
>          Components: JPA / Hibernate
>            Reporter: Anil Arora
>            Assignee: Scott Marlow
>
> Looking for a simple callback/listener mechanism so that we can invoke a database migration script before JPA persistent unit starts up and runs the Hibernate schema validation mechanism.
> Ideally, there would be a property within the persistence.xml that indicates which class to call.  It would also pass in/inject the datasource that is being used for the persistence unit. 
> Essentially, this would solve the lack of migration support in Hibernate itself.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira



More information about the jboss-jira mailing list