1. JPA-XJC

JPA-XJC

Introduction

JPA-XJC is a JAXB 2.1 XJC plugin for annotating schema derived classes with Java persistence annotations. It does not generate code depending on libraries other than JPA 1.0 and does not introduce non-standard customization elements. The plugin provides a '-jpa' option which is enabled by adding its jar file to the XJC classpath. When enabled, the following options can be used to control the behavior of the plugin.

  • -jpa unit-name unit-root

    The '-jpa' option itself takes two mandatory arguments. The 'unit-name' argument specifies the name of the persistence unit and the 'unit-root' argument specifies the name of the persistence unit root directory.

  • -persistenceSystemId (since 1.0-beta-6)

    The optional '-persistenceSystemId' option can be used to specify a system id to use in the 'schemaLocation' attribute of generated persistence unit XML documents. Default: http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd

  • -ormSystemId (since 1.0-beta-6)

    The optional '-ormSystemId' option can be used to specify a system id to use in the 'schemaLocation' attribute of generated orm XML documents. Default: http://java.sun.com/xml/ns/persistence/orm_1_0.xsd

Support

Development of JPA-XJC is community driven. Please file any issues with the MantisBT bugtracker or subscribe to the jpaxjc-users mailing list for discussing issues or asking questions. You may also want to use the wiki for providing content related to JPA-XJC yourself. Please note that applications provided by Sourceforge may require you to login to Sourceforge. If you do not have a Sourceforge account, you can create an account for free at Sourceforge.

Examples

Customizations

Customizations are needed whenever the default behavior of the plugin does not meet the persistence requirements. Customization is done by supplying one or more binding files (*.xjb) with elements from the http://java.sun.com/xml/ns/persistence and http://java.sun.com/xml/ns/persistence/orm namespaces. Instead of providing complete mapping files, only the elements for which no default could be determined need to be provided.

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:persistence="http://java.sun.com/xml/ns/persistence"
               xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
               version="2.0">

  <jaxb:bindings schemaLocation="po.xsd">
    <jaxb:bindings>
      <!-- Adds a jta-data-source element to the generated persistence.xml file. -->
      <persistence:jta-data-source>jta/po-model</persistence:jta-data-source>
    </jaxb:bindings>

    <jaxb:bindings node="xs:complexType[@name='PurchaseOrderType']">
      <!-- Adds a named query to the PurchaseOrderType entity. -->
      <orm:named-query name="find-whatever">
        <orm:query><![CDATA[SELECT p FROM PurchaseOrderType p WHERE whatever]]></orm:query>
      </orm:named-query>
    </jaxb:bindings>

    <jaxb:bindings node="xs:complexType[@name='PurchaseOrderType']/xs:sequence/xs:element[@ref='comment']">
      <!-- Changes the plugins' defaults of the comment property. -->
      <orm:basic>
        <orm:column length="4096" nullable="true"/>
      </orm:basic>
    </jaxb:bindings>
  </jaxb:bindings>
</jaxb:bindings>

JAXB-RI CLI

To use the JAXB-RI XJC command line interface simply add the corresponding java archives to the classpath and execute the XJC main class 'com.sun.tools.xjc.Driver'. The following example demonstrates a working command line for use with JDK 1.5 (assuming the needed dependencies are found in the current working directory).

  java -cp activation-1.1.jar:\
           jaxb-api-2.1.jar:\
           stax-api-1.0.jar:\
           jaxb-impl-2.1.12.jar:\
           jaxb-xjc-2.1.12.jar:\
           persistence-api-1.0.jar:\
           jpa-xjc-plugin-1.0-beta-7.jar\
           com.sun.tools.xjc.Driver -d /tmp/src -jpa "<name>" "<directory>" <schema files>

Maven

Maven users simply add the JPA-XJC plugin as a dependency to a JAXB plugin of choice. The following example demonstrates the use of the JPA-XJC Plugin with the Mojo jaxb2-maven-plugin.

As of 2010-02-26 the JPA-XJC artifacts are deployed to the central Maven respository.

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <version>1.3</version>
  <dependencies>
    <dependency>
      <groupId>net.sourceforge.jpaxjc</groupId>
      <artifactId>jpa-xjc-plugin</artifactId>
      <version>1.0-beta-7</version>
    </dependency>
  </dependencies>
  <executions>
    <execution>
      <id>xjc</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>xjc</goal>
      </goals>
      <configuration>
        <arguments>-jpa "${project.artifactId}" "${project.build.outputDirectory}"</arguments>
        <extension>true</extension>
      </configuration>
    </execution>
  </executions>
</plugin>

Download

Releases

Releases are deployed to the Sourceforge file release system. Please see the Sourceforge FRS. The changes report contains information regarding changes between releases.

License

The JPA-XJC Project Copyright

Copyright (c) 2009 The JPA-XJC Project. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE JPA-XJC PROJECT AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JPA-XJC PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.