Class MappingTool

  • All Implemented Interfaces:
    MetaDataModes

    public class MappingTool
    extends java.lang.Object
    implements MetaDataModes
    Tool for manipulating class mappings and associated schema.
    Author:
    Abe White
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  MappingTool.Flags
      Run flags.
      static interface  MappingTool.ImportExport
      Helper used to import and export mapping data.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Reset the internal repository.
      java.lang.String getAction()
      The action supplied on construction.
      boolean getDropUnusedComponents()
      Whether schema components that are unused by any mapping will be dropped from this tool's SchemaGroup, and, depending on the schema action, from the database.
      boolean getForeignKeys()
      Whether foreign keys on existing tables should be manipulated.
      boolean getIgnoreErrors()
      Whether and SQL errors should cause a failure or just issue a warning.
      boolean getIndexes()
      Whether indexes on existing tables should be manipulated.
      java.io.Writer getMappingWriter()
      The stream to export the planned mappings to as an XML document.
      java.io.File getMetaDataFile()
      If adding metadata, the metadata file to add to.
      boolean getPrimaryKeys()
      Whether primary keys on existing tables should be manipulated.
      boolean getReadSchema()
      Set to true to read the entire schema before mapping.
      MappingRepository getRepository()
      Return the repository to use to access mapping information.
      boolean getRollbackBeforeDDL()
      If true, rollback will be performed before each DDL statement is executed.
      java.lang.String getSchemaAction()
      The schema modification policy, or none.
      SchemaGroup getSchemaGroup()
      Return the schema group to use in mapping.
      java.io.Writer getSchemaWriter()
      The stream to export the planned schema to as an XML document.
      boolean getSequences()
      Whether to manipulate sequences.
      boolean isMetaDataAction()
      Whether the action works on metadata as well as mappings.
      static void main​(java.lang.String[] arguments)
      Usage: java org.apache.openjpa.jdbc.meta.MappingTool [option]* [-action/-a <refresh | add | buildSchema | drop | validate | import | export>] <class name | .java file | .class file | .jdo file>* Where the following options are recognized.
      void record()
      Records the changes that have been made to both the mappings and the associated schema, and clears the tool for further use.
      void record​(MappingTool.Flags flags)  
      void run​(java.lang.Class<?> cls)
      Run the configured action on the given instance.
      static boolean run​(JDBCConfiguration conf, java.lang.String[] args, MappingTool.Flags flags, java.lang.ClassLoader loader)
      Run the tool.
      static boolean run​(JDBCConfiguration conf, java.lang.String[] args, Options opts, java.lang.ClassLoader loader)
      Run the tool.
      void setDropUnusedComponents​(boolean dropUnused)
      Whether schema components that are unused by any mapping will be dropped from this tool's SchemaGroup, and, depending on the schema action, from the database.
      void setForeignKeys​(boolean fks)
      Whether foreign keys on existing tables should be manipulated.
      void setIgnoreErrors​(boolean ignoreErrors)
      Whether and SQL errors should cause a failure or just issue a warning.
      void setIndexes​(boolean indexes)
      Whether indexes on existing tables should be manipulated.
      void setMappingWriter​(java.io.Writer mappingWriter)
      The stream to export the planned mappings to as an XML document.
      void setMetaDataFile​(java.io.File file)
      If adding metadata, the metadata file to add to.
      void setPrimaryKeys​(boolean pks)
      Whether primary keys on existing tables should be manipulated.
      void setReadSchema​(boolean readSchema)
      Set to true to read the entire schema before mapping.
      void setRepository​(MappingRepository repos)
      Set the repository to use to access mapping information.
      void setRollbackBeforeDDL​(boolean rollbackBeforeDDL)
      If true, rollback will be performed before each DDL statement is executed.
      void setSchemaAction​(java.lang.String schemaAction)
      The schema modification policy, or none.
      void setSchemaGroup​(SchemaGroup schema)
      Set the schema to use in mapping.
      void setSchemaTool​(SchemaTool tool)
      Set the schema tool to use for schema modification.
      void setSchemaWriter​(java.io.Writer schemaWriter)
      The stream to export the planned schema to as an XML document.
      void setSequences​(boolean seqs)
      Whether to manipulate sequences.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MappingTool

        public MappingTool​(JDBCConfiguration conf,
                           java.lang.String action,
                           boolean meta)
        Constructor. Supply configuration and action.
      • MappingTool

        public MappingTool​(JDBCConfiguration conf,
                           java.lang.String action,
                           boolean meta,
                           java.lang.ClassLoader loader)
        Constructor. Supply configuration and action.
    • Method Detail

      • getAction

        public java.lang.String getAction()
        The action supplied on construction.
      • isMetaDataAction

        public boolean isMetaDataAction()
        Whether the action works on metadata as well as mappings.
      • getSchemaAction

        public java.lang.String getSchemaAction()
        The schema modification policy, or none. See the ACTION constants in SchemaTool. May be a comma-separated list of values. Defaults to SchemaTool.ACTION_ADD.
      • setSchemaAction

        public void setSchemaAction​(java.lang.String schemaAction)
        The schema modification policy, or none. See the ACTION constants in SchemaTool. May be a comma-separated list of values. Defaults to SchemaTool.ACTION_ADD.
      • getReadSchema

        public boolean getReadSchema()
        Set to true to read the entire schema before mapping. Leaving this option false saves time, but is dangerous when adding new mappings, because without full knowledge of the existing schema the mapping tool might create tables or indexes that conflict with existing components.
      • setReadSchema

        public void setReadSchema​(boolean readSchema)
        Set to true to read the entire schema before mapping. Leaving this option false saves time, but is dangerous when adding new mappings, because without full knowledge of the existing schema the mapping tool might create tables or indexes that conflict with existing components.
      • getSequences

        public boolean getSequences()
        Whether to manipulate sequences. Defaults to true.
      • setSequences

        public void setSequences​(boolean seqs)
        Whether to manipulate sequences. Defaults to true.
      • getIndexes

        public boolean getIndexes()
        Whether indexes on existing tables should be manipulated. Defaults to false.
      • setIndexes

        public void setIndexes​(boolean indexes)
        Whether indexes on existing tables should be manipulated. Defaults to false.
      • getForeignKeys

        public boolean getForeignKeys()
        Whether foreign keys on existing tables should be manipulated. Defaults to false.
      • setForeignKeys

        public void setForeignKeys​(boolean fks)
        Whether foreign keys on existing tables should be manipulated. Defaults to false.
      • getPrimaryKeys

        public boolean getPrimaryKeys()
        Whether primary keys on existing tables should be manipulated. Defaults to false.
      • setPrimaryKeys

        public void setPrimaryKeys​(boolean pks)
        Whether primary keys on existing tables should be manipulated. Defaults to false.
      • getDropUnusedComponents

        public boolean getDropUnusedComponents()
        Whether schema components that are unused by any mapping will be dropped from this tool's SchemaGroup, and, depending on the schema action, from the database. Defaults to true.
      • setDropUnusedComponents

        public void setDropUnusedComponents​(boolean dropUnused)
        Whether schema components that are unused by any mapping will be dropped from this tool's SchemaGroup, and, depending on the schema action, from the database. Defaults to true.
      • setIgnoreErrors

        public void setIgnoreErrors​(boolean ignoreErrors)
        Whether and SQL errors should cause a failure or just issue a warning.
      • getIgnoreErrors

        public boolean getIgnoreErrors()
        Whether and SQL errors should cause a failure or just issue a warning.
      • getRollbackBeforeDDL

        public boolean getRollbackBeforeDDL()
        If true, rollback will be performed before each DDL statement is executed. Defaults to true.
      • setRollbackBeforeDDL

        public void setRollbackBeforeDDL​(boolean rollbackBeforeDDL)
        If true, rollback will be performed before each DDL statement is executed. Defaults to true.
      • setSchemaTool

        public void setSchemaTool​(SchemaTool tool)
        Set the schema tool to use for schema modification.
      • getSchemaWriter

        public java.io.Writer getSchemaWriter()
        The stream to export the planned schema to as an XML document. If non-null, then the database schema will not be altered.
      • setSchemaWriter

        public void setSchemaWriter​(java.io.Writer schemaWriter)
        The stream to export the planned schema to as an XML document. If non-null, then the database schema will not be altered.
      • getMappingWriter

        public java.io.Writer getMappingWriter()
        The stream to export the planned mappings to as an XML document. If non-null, then the mapping repository will not be altered.
      • setMappingWriter

        public void setMappingWriter​(java.io.Writer mappingWriter)
        The stream to export the planned mappings to as an XML document. If non-null, then the mapping repository will not be altered.
      • getMetaDataFile

        public java.io.File getMetaDataFile()
        If adding metadata, the metadata file to add to.
      • setMetaDataFile

        public void setMetaDataFile​(java.io.File file)
        If adding metadata, the metadata file to add to.
      • setRepository

        public void setRepository​(MappingRepository repos)
        Set the repository to use to access mapping information.
      • getSchemaGroup

        public SchemaGroup getSchemaGroup()
        Return the schema group to use in mapping. If none has been set, the schema will be generated from the database.
      • setSchemaGroup

        public void setSchemaGroup​(SchemaGroup schema)
        Set the schema to use in mapping.
      • clear

        public void clear()
        Reset the internal repository. This is called automatically after every record().
      • record

        public void record()
        Records the changes that have been made to both the mappings and the associated schema, and clears the tool for further use. This also involves clearing the internal mapping repository.
      • run

        public void run​(java.lang.Class<?> cls)
        Run the configured action on the given instance.
      • main

        public static void main​(java.lang.String[] arguments)
                         throws java.io.IOException,
                                java.sql.SQLException
        Usage: java org.apache.openjpa.jdbc.meta.MappingTool [option]* [-action/-a <refresh | add | buildSchema | drop | validate | import | export>] <class name | .java file | .class file | .jdo file>* Where the following options are recognized.
        • -properties/-p <properties file or resource>: The path or resource name of a OpenJPA properties file containing information as outlined in OpenJPAConfiguration. Optional.
        • -<property name> <property value>: All bean properties of the OpenJPA JDBCConfiguration can be set by using their names and supplying a value. For example: -licenseKey adslfja83r3lkadf
        • -file/-f <stdout | output file or resource>: Use this option to write the planned mappings to an XML document rather than store them in the repository. This option also specifies the metadata file to write to if using the add action with the -meta true flag, or the file to dump to if using the export action.
        • -schemaAction/-sa <schema action | none>: The SchemaTool defines the actions possible. The actions will apply to all schema components used by the mappings involved. Unless you are running the mapping tool on all of your persistent types at once, be careful running schema actions that can drop data. It is possible to accidentally drop schema components that are used by classes you aren't currently running the tool over. The action defaults to add.
        • -schemaFile/-sf <stdout | output file or resource>: Use this option to write the planned schema to an XML document rather than modify the data store.
        • -sqlFile/-sql <stdout | output file or resource>: Use this option to write the planned schema changes as a SQL script rather than modifying the data store.
        • -sqlEncode/-se <encoding>: Use this option with the -sqlFile flag to write the SQL script in a different Java character set encoding than the default JVM locale, such as UTF-8.
        • -dropTables/-dt <true/t | false/f>: Corresponds to the same-named option in the SchemaTool.
        • -dropSequences/-dsq <true/t | false/f>: Corresponds to the same-named option in the SchemaTool.
        • -openjpaTables/-kt <true/t | false/f>: Corresponds to the same-named option in the SchemaTool.
        • -ignoreErrors/-i <true/t | false/f>: Corresponds to the same-named option in the SchemaTool.
        • -readSchema/-rs <true/t | false/f>: Set this to true to read the entire existing schema (even when false the parts of the schema used by classes the tool is run on will still be read). Turning on schema reading can ensure that no naming conflicts will occur, but it can take a long time.
        • -primaryKeys/-pk <true/t | false/f>: Whether primary keys on existing tables are manipulated. Defaults to false.
        • -foreignKeys/-fk <true/t | false/f>: Whether foreign keys on existing tables are manipulated. Defaults to false.
        • -indexes/-ix <true/t | false/f>: Whether indexes on existing tables are manipulated. Defaults to false.
        • -sequences/-sq <true/t | false/f>: Whether sequences are manipulated. Defaults to true.
        • -schemas/-s <schema and table names>: A list of schemas and/or tables to read. Corresponds to the same-named option in the SchemaGenerator. This option is ignored if readSchema is false.
        • -meta/-m <true/t | false/f>: Whether the given action applies to metadata as well as mappings.
        The various actions are as follows.
        • refresh: Bring the mapping information up-to-date with the class definitions. OpenJPA will attempt to use any provided mapping information, and fill in missing information. If the provided information conflicts with the class definition, the conflicting information will be discarded and the class/field will be re-mapped to new columns/tables. This is the default action.
        • add: If used with the -meta option, adds new default metadata for the given class(es). Otherwise, brings the mapping information up-to-date with the class definitions. OpenJPA will attempt to use any provided mapping information, and fill in missing information. OpenJPA will fail if the provided information conflicts with the class definition.
        • buildSchema: Create the schema matching the existing mappings for the given class(es). Any invalid mapping information will cause an exception.
        • drop: Delete mappings for the given classes. If used with the -meta option, also deletes metadata.
        • validate: Validate the given mappings. The mapping repository and schema will not be affected.
        • import: Import mappings from an XML document and store them as the current system mappings.
        • export: Dump the current mappings for the given classes to an XML document specified by the file option.
        • If used with the -meta option, the metadata will be included in the export.
        Each class supplied as an argument must have valid metadata. If no class arguments are given, the tool runs on all metadata files in the CLASSPATH. Examples:
        • Refresh the mappings for given package, without dropping any schema components:
          java org.apache.openjpa.jdbc.meta.MappingTool mypackage.jdo
        • Refresh the mappings for all persistent classes in the classpath, dropping any unused columns and even tables:
          java org.apache.openjpa.jdbc.meta.MappingTool -sa refresh -dt true
        • Make sure the mappings you've created by hand match the object model and schema:
          java org.apache.openjpa.jbdc.meta.MappingTool -a validate Person.java
        • Remove the recorded mapping for a given class:
          java org.apache.openjpa.jbdc.meta.MappingTool -a drop Person.java
        • Record the current mappings in an XML file:
          java org.apache.openjpa.jdbc.meta.MappingTool -f mypackage.orm -a export mypackage.jdo
        Throws:
        java.io.IOException
        java.sql.SQLException
      • run

        public static boolean run​(JDBCConfiguration conf,
                                  java.lang.String[] args,
                                  Options opts,
                                  java.lang.ClassLoader loader)
                           throws java.io.IOException,
                                  java.sql.SQLException
        Run the tool. Returns false if invalid options are given.
        Throws:
        java.io.IOException
        java.sql.SQLException
        See Also:
        main(java.lang.String[])
      • run

        public static boolean run​(JDBCConfiguration conf,
                                  java.lang.String[] args,
                                  MappingTool.Flags flags,
                                  java.lang.ClassLoader loader)
                           throws java.io.IOException,
                                  java.sql.SQLException
        Run the tool. Return false if an invalid option was given.
        Throws:
        java.io.IOException
        java.sql.SQLException