wsreversemapping command
The wsreversemapping tool generates persistent class definitions and metadata from a database schema.
Syntax
Before running the command, have a copy of persistence.xml on the classpath, or specify it as a properties file through the -p [path_to_persistence.xml] argument. Issue the command from the bin subdirectory of the app_install_root directory.The command syntax is as follows:
[AIX] [HP-UX] [Linux] [Solaris]
wsreversemapping.sh [parameters][arguments]
(Windows)
wsreversemapping.bat [parameters][arguments]
Parameters
The wsreversemapping tool accepts the standard set of command-line arguments defined by the configuration framework along with the following:
- -schemas/-s <schema and table names>: A list of schema and table names, separated by commas, to run the reversmapping tool on if no XML schema file is supplied. Each element of the list must follow the naming conventions for the openjpa.jdbc.Schemas property. If this parameter flag is omitted, it defaults to the value of the Schemas property. If the Schemas property is not defined, then all schemas will be reverse mapped.
- -package/-p <package name>: The package name of the generated classes. If no package name is given, the generated code will not contain package declarations.
- -directory/-d <output directory>: All generated code and metadata will be written to the directory at this path. If the path does not match the package of a class, the package structure will be created beneath this directory. This parameter defaults to the current directory.
- -useSchemaName/-sn <true/t | false/f>: Set this parameter flag to true to include the schema as well as the table name in the name of each generated class. This can be useful when dealing with multiple schemas that have tables with identical names.
- -useForeignKeyName/-fkn <true/t | false/f>: Set this parameter flag to true if we want the field names for relations to be based on the database foreign key name. By default, relation field names are derived from the name of the related class.
- -nullableAsObject/-no <true/t | false/f>: By default, all non-foreign key columns are mapped to primitives. Set this parameter flag to true to generate primitive wrapper fields instead for columns that allow null values.
- -blobAsObject/-bo <true/t | false/f>: By default, all binary columns are mapped to the byte[] fields. Set this parameter flag to true to map them to Object fields instead.
When mapped this way, the column is presumed to contain a serialized Java object.
- -primaryKeyOnJoin/pkj <true/t | false/f>: The standard reverse mapping tool behavior is to map all tables with primary keys to persistent classes. If the schema has primary keys on many join tables as well, set this flag to true to avoid creating classes for those tables.
- -inverseRelations/-ir <true/t | false/f>: Set this parameter flag to false to prevent the creation of inverse one-to-many/one-to-one relations for every many-to-one/one-to-one relation detected.
- -useDatastoreIdentity/-ds <true/t | false/f>: Set to true to use datastore identity for tables that have single numeric primary key columns. The tool typically uses application identity for all generated classes.
- -useBuiltinIdentityClass/-bic <true/t | false/f>: Set this parameter flag to false to prevent the reversemapping tool from using built-in application identity classes when possible. This will force the tool to create custom application identity classes even when there is only one primary key column.
- -innerIdentityClasses/-inn <true/t | false/f>: Set this parameter flag to true to have any generated application identity classed be created as static inner classes within the persistent classes. The default setting is false.
- -identityClassSuffix/-is <suffix>: Suffix to append to the class names to form application identity class names, or for inner identity classes, the inner class name. The default suffix is Id.
- -typeMap/-typ <type mapping>: A string that specifies the default Java classes to generate for each SQL type that is seen in the schema. The format is SQLTYPE1=Java Class1, SQLTYPE2=Java Class2. The SQL type name first looks for a customization that is based on SQLTYPE(SIZE,PRECISION), then SQLTYPE(SIZE), and then SQLTYPE. If a column with type CHAR is found, it will first look for the CHAR(50,0) type name specification, then it will look for the CHAR(50), and finally it will look for the CHAR. For example, to generate a char array for every char column whose size is exactly 50 characters, and to generate a short for every type name of INTEGER, we might specify: CHAR(50)=char[],INTEGER=short.
Various databases report different type names differently, one database type may not work for another database. Enable TRACE level logging on the MetaData channel to track which type names JPA for WAS is examining.
- -customizerClass/-cc <class name>: The full class name of an org.apache.openjpa.jdbc.meta.ReverseCustomizer customization plugin. If we do not specify a reverse customizer of the own, the system defaults to a PropertiesReverseCustomizer. This customizer allows you to specify simple customization options in the properties file given with the -customizerProperties flag.
- -customizerProperties/-cp<properties file or resource>: The path or resource name of a properties file to pass to the reverse customizer on initialization.
- -customizer/-c <property name> <property value>: The given property name will be matched with the corresponding Java bean property in the specified reverse customizer, and set to the given value.
Usage
The wsreversemapping tool is used to perform reverse (bottom-up) mappings of database tables to entity source files. This is useful if developers want to generate Java files from a database for use in other JPA applications. To run this tool:
- we need to have database tables and the database connection configured.
- Run the wsreversemapping tool from the command line in the $ {WAS_HOME}/bin directory.
- The tool will generate .java files for every class along with a XML descriptor file orm.xml
The generated Java files from the wsreversemapping tool might require some editing before they can be used in an application. Also, generated files will not contain annotations. Annotations can be added manually if desired. Messages and errors are logged to the console as specified by the configuration.
Examples
Generate entities based on the information saved in the schema.xml file. Schema.xml was created by running the schema tool. The Java files are created in the src directory and use the package com.abc: [AIX] [HP-UX] [Linux] [Solaris]
${WAS_HOME}/bin/wsreversemapping.sh -pkg com.abc -d ./src schema.xml
(Windows)
${WAS_HOME}\bin\wsreversemapping.bat -pkg com.abc -d ./src schema.xmlGenerate entities based on information in a DB2 database. Entities are created in the src directory, and use the package com.reversemapped: [AIX] [HP-UX] [Linux] [Solaris]
C:\> %WAS_HOME%/bin/wsreversemapping.bat -sa dropDB Magazine.javapkg com.reversemapped -d src -connectionDriverName=com.ibm.db2.jcc.DB2Driver -connectionURL=jdbc:db2:localhost:50000/TEST -connectionUser=db2User -connectionPassword=db2Password
(Windows)
C:\> %WAS_HOME%\bin\wsreversemapping.bat -sa dropDB Magazine.javapkg com.reversemapped -d src -connectionDriverName=com.ibm.db2.jcc.DB2Driver -connectionURL=jdbc:db2:localhost:50000/TEST -connectionUser=db2User -connectionPassword=db2Password
Additional information
See, consult the mapping section in the Apache OpenJPA User's Guide.
Related tasks
Develop and packaging JPA applications for a Java SE environment
Related
Apache OpenJPA User's Guide: Mapping