| Method from org.apache.openjpa.jdbc.meta.MappingTool Detail: |
public void clear() {
_repos = null;
_schema = null;
_schemaTool = null;
_flush = false;
_flushSchema = false;
if (_dropCls != null)
_dropCls.clear();
if (_dropMap != null)
_dropMap.clear();
}
Reset the internal repository. This is called automatically after
every #record . |
public String getAction() {
return _action;
}
The action supplied on construction. |
public boolean getDropUnusedComponents() {
return _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. |
public boolean getForeignKeys() {
return _fks;
}
Whether foreign keys on existing tables should be manipulated.
Defaults to false. |
public boolean getIgnoreErrors() {
return _ignoreErrors;
}
Whether and SQL errors should cause a failure or just issue a warning. |
public boolean getIndexes() {
return _indexes;
}
Whether indexes on existing tables should be manipulated.
Defaults to false. |
public Writer getMappingWriter() {
return _mappingWriter;
}
The stream to export the planned mappings to as an XML document.
If non-null, then the mapping repository will not be altered. |
public File getMetaDataFile() {
return _file;
}
If adding metadata, the metadata file to add to. |
public boolean getPrimaryKeys() {
return _pks;
}
Whether primary keys on existing tables should be manipulated.
Defaults to false. |
public boolean getReadSchema() {
return _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. |
public MappingRepository getRepository() {
if (_repos == null) {
_repos = _conf.newMappingRepositoryInstance();
_repos.setSchemaGroup(getSchemaGroup());
_repos.setValidate(_repos.VALIDATE_UNENHANCED, false);
}
return _repos;
}
Return the repository to use to access mapping information.
Defaults to a new MappingRepository . |
public String getSchemaAction() {
return _schemaActions;
}
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 . |
public SchemaGroup getSchemaGroup() {
if (_schema == null) {
if (_action.indexOf(ACTION_BUILD_SCHEMA) != -1) {
DynamicSchemaFactory factory = new DynamicSchemaFactory();
factory.setConfiguration(_conf);
_schema = factory;
} else if (_readSchema
|| contains(_schemaActions,SchemaTool.ACTION_RETAIN)
|| contains(_schemaActions,SchemaTool.ACTION_REFRESH)) {
_schema = (SchemaGroup) newSchemaTool(null).getDBSchemaGroup().
clone();
} else {
// with this we'll just read tables as different mappings
// look for them
LazySchemaFactory factory = new LazySchemaFactory();
factory.setConfiguration(_conf);
factory.setPrimaryKeys(getPrimaryKeys());
factory.setForeignKeys(getForeignKeys());
factory.setIndexes(getIndexes());
_schema = factory;
}
if (_schema.getSchemas().length == 0)
_schema.addSchema();
}
return _schema;
}
Return the schema group to use in mapping. If none has been set, the
schema will be generated from the database. |
public Writer getSchemaWriter() {
return _schemaWriter;
}
The stream to export the planned schema to as an XML document.
If non-null, then the database schema will not be altered. |
public boolean getSequences() {
return _seqs;
}
Whether to manipulate sequences. Defaults to true. |
public boolean isMetaDataAction() {
return _meta;
}
Whether the action works on metadata as well as mappings. |
public static void main(String[] arguments) throws IOException, SQLException {
Options opts = new Options();
final String[] args = opts.setFromCmdLine(arguments);
boolean ret = Configurations.runAgainstAllAnchors(opts,
new Configurations.Runnable() {
public boolean run(Options opts) throws IOException, SQLException {
JDBCConfiguration conf = new JDBCConfigurationImpl();
try {
return MappingTool.run(conf, args, opts);
} finally {
conf.close();
}
}
});
if (!ret)
System.err.println(_loc.get("tool-usage"));
}
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.
- -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
|
public void record() {
record(null);
}
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. |
public void run(Class cls) {
if (ACTION_ADD.equals(_action)) {
if (_meta)
addMeta(cls);
else
add(cls);
} else if (ACTION_REFRESH.equals(_action))
refresh(cls);
else if (ACTION_BUILD_SCHEMA.equals(_action))
buildSchema(cls);
else if (ACTION_DROP.equals(_action))
drop(cls);
else if (ACTION_VALIDATE.equals(_action))
validate(cls);
}
Run the configured action on the given instance. |
public static boolean run(JDBCConfiguration conf,
String[] args,
Options opts) throws IOException, SQLException {
// flags
Flags flags = new Flags();
flags.action = opts.removeProperty("action", "a", flags.action);
flags.schemaAction = opts.removeProperty("schemaAction", "sa",
flags.schemaAction);
flags.dropTables = opts.removeBooleanProperty
("dropTables", "dt", flags.dropTables);
flags.openjpaTables = opts.removeBooleanProperty
("openjpaTables", "ot", flags.openjpaTables);
flags.dropSequences = opts.removeBooleanProperty
("dropSequences", "dsq", flags.dropSequences);
flags.readSchema = opts.removeBooleanProperty
("readSchema", "rs", flags.readSchema);
flags.primaryKeys = opts.removeBooleanProperty
("primaryKeys", "pk", flags.primaryKeys);
flags.indexes = opts.removeBooleanProperty("indexes", "ix",
flags.indexes);
flags.foreignKeys = opts.removeBooleanProperty("foreignKeys", "fk",
flags.foreignKeys);
flags.sequences = opts.removeBooleanProperty("sequences", "sq",
flags.sequences);
flags.ignoreErrors = opts.removeBooleanProperty
("ignoreErrors", "i", flags.ignoreErrors);
flags.meta = opts.removeBooleanProperty("meta", "m", flags.meta);
String fileName = opts.removeProperty("file", "f", null);
String schemaFileName = opts.removeProperty("schemaFile", "sf", null);
String sqlFileName = opts.removeProperty("sqlFile", "sql", null);
String schemas = opts.removeProperty("s");
if (schemas != null)
opts.setProperty("schemas", schemas);
Configurations.populateConfiguration(conf, opts);
ClassLoader loader = conf.getClassResolverInstance().
getClassLoader(MappingTool.class, null);
if (flags.meta && ACTION_ADD.equals(flags.action))
flags.metaDataFile = Files.getFile(fileName, loader);
else
flags.mappingWriter = Files.getWriter(fileName, loader);
flags.schemaWriter = Files.getWriter(schemaFileName, loader);
flags.sqlWriter = Files.getWriter(sqlFileName, loader);
return run(conf, args, flags, loader);
}
Run the tool. Returns false if invalid options are given. |
public static boolean run(JDBCConfiguration conf,
String[] args,
MappingTool.Flags flags,
ClassLoader loader) throws IOException, SQLException {
// default action based on whether the mapping defaults fills in
// missing info
if (flags.action == null) {
if (conf.getMappingDefaultsInstance().defaultMissingInfo())
flags.action = ACTION_BUILD_SCHEMA;
else
flags.action = ACTION_REFRESH;
}
// collect the classes to act on
Log log = conf.getLog(OpenJPAConfiguration.LOG_TOOL);
Collection classes = null;
if (args.length == 0) {
if (ACTION_IMPORT.equals(flags.action))
return false;
log.info(_loc.get("running-all-classes"));
classes = conf.getMappingRepositoryInstance().
loadPersistentTypes(true, loader);
} else {
classes = new HashSet();
ClassArgParser classParser = conf.getMetaDataRepositoryInstance().
getMetaDataFactory().newClassArgParser();
classParser.setClassLoader(loader);
Class[] parsed;
for (int i = 0; args != null && i < args.length; i++) {
parsed = classParser.parseTypes(args[i]);
classes.addAll(Arrays.asList(parsed));
}
}
Class[] act = (Class[]) classes.toArray(new Class[classes.size()]);
if (ACTION_EXPORT.equals(flags.action)) {
// run exports until the first export succeeds
ImportExport[] instances = newImportExports();
for (int i = 0; i < instances.length; i++) {
if (instances[i].exportMappings(conf, act, flags.meta, log,
flags.mappingWriter))
return true;
}
return false;
}
if (ACTION_IMPORT.equals(flags.action)) {
// run exports until the first export succeeds
ImportExport[] instances = newImportExports();
for (int i = 0; i < instances.length; i++) {
if (instances[i].importMappings(conf, act, args, flags.meta,
log, loader))
return true;
}
return false;
}
MappingTool tool;
try {
tool = new MappingTool(conf, flags.action, flags.meta);
} catch (IllegalArgumentException iae) {
return false;
}
// setup the tool
tool.setIgnoreErrors(flags.ignoreErrors);
tool.setMetaDataFile(flags.metaDataFile);
tool.setMappingWriter(flags.mappingWriter);
tool.setSchemaAction(flags.schemaAction);
tool.setSchemaWriter(flags.schemaWriter);
tool.setReadSchema(flags.readSchema
&& !ACTION_VALIDATE.equals(flags.action));
tool.setPrimaryKeys(flags.primaryKeys);
tool.setForeignKeys(flags.foreignKeys);
tool.setIndexes(flags.indexes);
tool.setSequences(flags.sequences || flags.dropSequences);
// and run the action
for (int i = 0; i < act.length; i++) {
log.info(_loc.get("tool-running", act[i], flags.action));
if (i == 0 && flags.readSchema)
log.info(_loc.get("tool-time"));
tool.run(act[i]);
}
log.info(_loc.get("tool-record"));
tool.record(flags);
return true;
}
Run the tool. Return false if an invalid option was given. |
public void setDropUnusedComponents(boolean dropUnused) {
_dropUnused = 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. |
public void setForeignKeys(boolean fks) {
_fks = fks;
}
Whether foreign keys on existing tables should be manipulated.
Defaults to false. |
public void setIgnoreErrors(boolean ignoreErrors) {
_ignoreErrors = ignoreErrors;
}
Whether and SQL errors should cause a failure or just issue a warning. |
public void setIndexes(boolean indexes) {
_indexes = indexes;
}
Whether indexes on existing tables should be manipulated.
Defaults to false. |
public void setMappingWriter(Writer mappingWriter) {
_mappingWriter = mappingWriter;
}
The stream to export the planned mappings to as an XML document.
If non-null, then the mapping repository will not be altered. |
public void setMetaDataFile(File file) {
_file = file;
}
If adding metadata, the metadata file to add to. |
public void setPrimaryKeys(boolean pks) {
_pks = pks;
}
Whether primary keys on existing tables should be manipulated.
Defaults to false. |
public void setReadSchema(boolean readSchema) {
_readSchema = 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. |
public void setRepository(MappingRepository repos) {
_repos = repos;
}
Set the repository to use to access mapping information. |
public void setSchemaAction(String schemaAction) {
_schemaActions = 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 . |
public void setSchemaGroup(SchemaGroup schema) {
_schema = schema;
}
Set the schema to use in mapping. |
public void setSchemaTool(SchemaTool tool) {
_schemaTool = tool;
}
Set the schema tool to use for schema modification. |
public void setSchemaWriter(Writer schemaWriter) {
_schemaWriter = schemaWriter;
}
The stream to export the planned schema to as an XML document.
If non-null, then the database schema will not be altered. |
public void setSequences(boolean seqs) {
_seqs = seqs;
}
Whether to manipulate sequences. Defaults to true. |