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>
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 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>