JPOX
JPOX
 Project  |  Ver 1.1  |  Ver 1.2  |  JDO  |  JPA  |  Guides  |  Tools
1.2 | Persistence | JDO ORM | JPA ORM | Runtime | JDO Runtime | JPA Runtime | Extensions | Developer
JPOX Runtime
RDBMS Datastores
DB4O Datastore
SchemaTool

The meta-data files and annotations define how a class maps across to a persistent store (e.g database). The database schema can be

  • Already existing, and so the user maps their classes to their existing tables
  • Manually created by the user.
  • Automatically created at runtime by use of the org.jpox.autoCreateSchema property.
  • Created before running the application, using JPOX SchemaTool.

We will describe here the use of JPOX SchemaTool. It's included in the JPOX Core jar, and is very simple to operate.

JPOX SchemaTool has the following modes of operation :

  • create - create all database tables required for the classes defined by the input data.
  • delete - delete all database tables required for the classes defined by the input data.
  • validate - validate all database tables required for the classes defined by the input data.
  • dbinfo - provide detailed information about the database, it's limits and datatypes support.
  • schemainfo - provide detailed information about the database schema.
In addition, the create mode can be used by adding "-ddlFile {filename}" and this will then not create the schema, but instead output the DDL for the tables into the specified file.

For the create, delete and validate modes JPOX SchemaTool accepts either of the following types of input.

  • A set of MetaData and class files. The MetaData files define the persistence of the classes they contain. The class files are provided when the classes have annotations. This is what was supported in JPOX 1.1
  • The name of a persistence-unit. The persistence-unit name defines all classes, metadata files, and jars that make up that unit. Consequently, running JPOX SchemaTool with a persistence unit name will create the schema for all classes that are part of that unit.

Here we provide many different ways to invoke JPOX SchemaTool



Manual Usage

If you wish to call JPOX SchemaTool manually, it can be called as follows

java [-cp classpath] [system_props] org.jpox.SchemaTool [modes] [options] [props] [jdo-files] [class-files]
    where system_props (when specified) should include
        -Djavax.jdo.option.ConnectionDriverName=db_driver_name
        -Djavax.jdo.option.ConnectionURL=db_url
        -Djavax.jdo.option.ConnectionUserName=db_username
        -Djavax.jdo.option.ConnectionPassword=db_password
        -Djavax.jdo.option.Mapping=orm_mapping_name (optional)
        -Dlog4j.configuration=file:{log4j.properties} (optional)
    where modes can be
        -create : Create the tables specified by the jdo-files/class-files
        -delete : Delete the tables specified by the jdo-files/class-files
        -validate : Validate the tables specified by the jdo-files/class-files
        -dbinfo : Detailed information about the database
        -schemainfo : Detailed information about the database schema
    where options can be
        -ddlFile {filename} : only for use with "create" mode to dump the DDL to the specified file
        -completeDdl : when using "ddlFile" to get all DDL output and not just missing tables/constraints
        -api : The API that is being used (default is JDO, but can be set to JPA)
        -persistenceUnit {persistence-unit-name} : Name of the persistence unit to manage the schema for
        -v : verbose output
    where props can be
        -props {propsfilename} : PMF properties to use in place of the "system_props"
                

All classes, MetaData files, "persistence.xml" files must be present in the CLASSPATH. In terms of the schema to use, you either specify the "props" file (recommended), or you specify the System properties defining the database connection. You should only specify one of the [modes] above. Let's make a specific example and see the output from SchemaTool. So we have the following files in our application

src/java/...   (source files and MetaData files)
target/classes/...    (enhanced classes, and MetaData files)
lib/log4j.jar
lib/jpox-core.jar
lib/jpox-rdbms.jar
lib/jdo2-api.jar
lib/mysql-connector-java.jar    (JDBC driver for our database)
log4j.properties

So we want to create the schema for our persistent classes. So let's invoke JPOX SchemaTool to do this, from the top level of our project. In this example we're using Linux (change the CLASSPATH definition to suit for Windows)

jpox -cp target/classes:lib/log4j.jar:lib/jdo2-api.jar:lib/jpox-core.jar:lib/jpox-rdbms.jar:lib/mysql-connector-java.jar
      -Dlog4j.configuration=file:log4j.properties
      org.jpox.SchemaTool -create
      -props jpox.properties
      target/classes/org/jpox/examples/normal/package.jdo
      target/classes/org/jpox/examples/inverse/package.jdo


JPOX SchemaTool (version 1.2.0) : Creation of the schema

JPOX SchemaTool : Classpath
>>  /home/andy/work/JPOX/samples/packofcards/target/classes
>>  /home/andy/work/JPOX/samples/packofcards/lib/log4j.jar
>>  /home/andy/work/JPOX/samples/packofcards/lib/jpox-core.jar
>>  /home/andy/work/JPOX/samples/packofcards/lib/jpox-rdbms.jar
>>  /home/andy/work/JPOX/samples/packofcards/lib/jdo2-api.jar
>>  /home/andy/work/JPOX/samples/packofcards/lib/mysql-connector-java.jar

JPOX SchemaTool : Input Files
>> /home/andy/work/JPOX/samples/packofcards/target/classes/org/jpox/examples/inverse/package.jdo
>> /home/andy/work/JPOX/samples/packofcards/target/classes/org/jpox/examples/normal/package.jdo

JPOX SchemaTool : Taking JDO properties from file "jpox.properties"

SchemaTool completed successfully

So as you see, JPOX SchemaTool prints out our input, the properties used, and finally a success message. If an error occurs, then something will be printed to the screen, and more information will be written to the log.



Maven1

If you are using Maven1 to build your system, you will need the JPOX Maven plugin. This provides 3 goals representing the different modes of JPOX SchemaTool. You can use the goals jpox:schema-create, jpox:schema-delete, jpox:schema-validate depending on whether you want to create, delete or validate the database tables. To use the JPOX Maven1 plugin you will need to set properties for the plugin (in your project.properties). For example

maven.jpox.verbose=true
maven.jpox.schematool.ddlfile=
maven.jpox.orm.mapping=mysql
maven.jpox.log4j.configuration=file:log4j.properties
maven.jpox.properties=${basedir}/jpox.properties
                

So with these example properties I am setting the Maven plugin to use PMF properties from the file "jpox.properties" at the root of the Maven project. I am also specifying a log4j configuration file defining the logging for the SchemaTool process. In addition, I am specifying my object-relational mapping (O/R mapping) in files with names like package-mysql.orm. This last part is optional, and you can specify the mapping information in the JDO MetaData files. The other property we have defined is to receive verbose output from SchemaTool. The output obtained from Maven like this is identical to that obtained when invoking the SchemaTool manually.



Maven2

If you are using Maven2 to build your system, you will need the JPOX Maven2 plugin. This provides 5 goals representing the different modes of JPOX SchemaTool. You can use the goals jpox:schema-create, jpox:schema-delete, jpox:schema-validate depending on whether you want to create, delete or validate the database tables. To use the JPOX Maven2 plugin you will may need to set properties for the plugin (in your pom.xml). For example

Configuration name   Default Value     Description
mappingIncludes      **/*.jdo          Fileset to include
mappingExcludes                        Fileset to exclude, if any
props                                  Name of a properties file defining the datastore
outputFile                             name of a file to dump DDL to (when using schema-create)
log4jConfiguration   {internal props}  Log definition to use
persistenceUnitName                    Name of the persistence-unit to generate the schema for
api                  JDO               API to use
verbose              false             Turn on more output ?

So to give an example, I add the following to my pom.xml

    <build>
        ...
        <plugins>
            <plugin>
                <groupId>jpox</groupId>
                <artifactId>jpox-maven-plugin</artifactId>
                <version>1.2.0-beta-5</version>
                <configuration>
                    <props>${basedir}/jpox.properties</props>
                    <log4jConfiguration>${basedir}/log4j.properties</log4jConfiguration>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
        ...
    </build>

So with these properties when I run SchemaTool it uses properties from the file "jpox.properties" at the root of the Maven project. I am also specifying a log4j configuration file defining the logging for the SchemaTool process. I then can invoke any of the Maven2 goals

mvn jpox:schema-create              Create the Schema
mvn jpox:schema-delete              Delete the schema
mvn jpox:schema-validate            Validate the Schema
mvn jpox:schema-info                Output info for the Schema
mvn jpox:schema-dbinfo              Output info for the datastore


Ant

An Ant task is provided for using JPOX SchemaTool. It has classname org.jpox.SchemaToolTask, and accepts the following parameters

ParameterDescriptionvalues
modeMode of operation.create, delete, validate, dbinfo, schemainfo
verboseWhether to give verbose output.true, false
propsThe filename to use for PMF properties
ddlFileThe filename where SchemaTool should output the DDL.
completeDdlWhether to output complete DDL (instead of just missing tables). Only used with ddlFiletrue, false
apiAPI that we are using in our use of JPOXJDO | JPA
persistenceUnitName of the persistence-unit that we should manage the schema for

In addition to the parameters that the Ant task accepts, you will need to set up your CLASSPATH to include the classes and JDO MetaData files, and to define the following system properties via the sysproperty parameter (not required when specifying the PMF props via the properties file)

ParameterDescriptionOptional
javax.jdo.option.ConnectionDriverNameName of JDBC driver classMandatory
javax.jdo.option.ConnectionURLURL for the databaseMandatory
javax.jdo.option.ConnectionUserNameUser name for the databaseMandatory
javax.jdo.option.ConnectionPasswordPassword for the databaseMandatory
javax.jdo.option.MappingORM Mapping nameOptional
log4j.configurationLog4J configuration file, for SchemaTool's LogOptional

So you could define something like the following, setting up the parameters schematool.classpath, javax.jdo.option.ConnectionDriverName, javax.jdo.option.ConnectionURL, javax.jdo.option.ConnectionUserName, and javax.jdo.option.ConnectionPassword to suit your situation.

You define the jdo files to create the tables using fileset.

<taskdef name="schematool" classname="org.jpox.SchemaToolTask" />

<schematool failonerror="true" verbose="true" mode="create">
    <classpath>
        <path refid="schematool.classpath"/>
    </classpath>
    <fileset dir="${classes.dir}">
        <include name="**/*.jdo"/>
    </fileset>
    <sysproperty key="javax.jdo.option.ConnectionDriverName" 
                 value="${javax.jdo.option.ConnectionDriverName}"/>
    <sysproperty key="javax.jdo.option.ConnectionURL" 
                 value="${javax.jdo.option.ConnectionURL}"/>
    <sysproperty key="javax.jdo.option.ConnectionUserName" 
                 value="${javax.jdo.option.ConnectionUserName}"/>
    <sysproperty key="javax.jdo.option.ConnectionPassword" 
                 value="${javax.jdo.option.ConnectionPassword}"/>
    <sysproperty key="javax.jdo.option.Mapping" 
                 value="${javax.jdo.option.Mapping}"/>
</schematool>
Jython

Jython is a powerful scripting language that allows you to automate tasks and easy the development. If you are using Jython, and wants to use JPOX tools, all you need is to the place the JPOX jars, the persistent classes, metadata files, jdbc driver jars and dependencies into the classpath.

The Jython script may be written in several forms but achieving the same goals.

Here we have a template for invoking the main method of the Schema Tool. The main method acts like the command line, by parsing arguments and invoking the appropriate methods.

from org.jpox import SchemaTool
tool = SchemaTool()
tool.main(<<<[options] [jdo-files]>>>)
The below is a concrete example.
from org.jpox import SchemaTool
tool = SchemaTool()
tool.main(["-create",
           "-props=/home/JDOproperties.properties",
           "target/classes/org/jpox/examples/normal/package.jdo",
           "target/classes/org/jpox/examples/inverse/package.jdo"])

For other operations of the SchemaTool consult the JPOX javadocs. For questions about Jython, please refer to Jython WebSite.

SchemaTool API

JPOX SchemaTool can also be called programmatically from an application. The API is shown below.

package org.jpox;

public class SchemaTool
{
     public static int createSchema(PersistenceManagerFactory pmf, List classNames, String ddlFilename)

     public static int deleteSchema(PersistenceManagerFactory pmf, List classNames)

     public static int validateSchema(PersistenceManagerFactory pmf, List classNames)

}

So for example to create the schema for classes mydomain.A and mydomain.B you would do

PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("jpox.properties");
...
List classNames = new ArrayList();
classNames.add("mydomain.A");
classNames.add("mydomain.B");
try
{
    org.jpox.SchemaTool.createSchema(pmf, classNames, null);
}
catch(Exception e)
{
    ...
}