Generate JPA Java classes from XSD schema file using Maven

The standard JDK command xjc will handily generate Java classes from a given xsd file:

"%java_home%\bin\xjc" -p [your package namespace] [xsd_file].xsd

…but if you want to specify information such as a super-class or Java perisitence for the generated classes, the following info should help.

The following example uses the Hyperjaxb3 plugin. Hyperjaxb3 is an open source project which provides relational persistence for JAXB objects.

To begin, in a maven Jar type project, place your .xsd file under src/main/resources e.g. take a simple xsd like the Shipment Odrer XSD (shiporder.xsd) from: http://www.w3schools.com/schema/schema_example.asp

In the same folder src/main/resources, create a binding file called e.g. binding.xjb:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jxb:bindings version="1.0"
              xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
  
	<jxb:bindings schemaLocation="shiporder.xsd" node="/xs:schema">  

		<jxb:globalBindings localScoping="toplevel">
        	<xjc:superClass name="com.pat.demos.xjc.MyAbstractBaseType"/>        	
        	<jxb:serializable/>
    	</jxb:globalBindings>
			  			  
		<jxb:schemaBindings>
  			<jxb:package name="com.pat.demo.shiporder.domain"></jxb:package>  
		</jxb:schemaBindings>			  
         
 	</jxb:bindings>
 
</jxb:bindings>

Here I’ve specified a super class called com.pat.demos.xjc.MyAbstractBaseType. I’ve also marked the classes to be generated as serializable and belonging to the package com.pat.demo.shiporder.domain.

Next, specify the hyperjaxb3 plugin in the pom. I’ve specified that old output should be removed and that the generated classes will extend from a super-class.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>jaxb-from-pom_demo</groupId>
	<artifactId>jaxb-from-pom_demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<build>
		<plugins>

			<!-- Generates classes from .xsd file and annotates them as persistent 
				entities: -->
			<plugin>
				<groupId>org.jvnet.hyperjaxb3</groupId>
				<artifactId>maven-hyperjaxb3-plugin</artifactId>
				<version>0.5.6</version>
				<executions>
					<execution>										
						<!-- for the generate goal of hyperjaxb3, use the following config: -->
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<variant>jpa2</variant>
							<!-- allows superclass to be defined for the classes generated from 
								xsd: -->
							<extension>true</extension>
							<removeOldOutput>true</removeOldOutput>
						</configuration>
					</execution>
				</executions>
			</plugin>

		</plugins>
	</build>

	<!-- these dependencies are needed to compile the generated classes: -->
	<dependencies>

		<dependency>
			<groupId>javax.persistence</groupId>
			<artifactId>persistence-api</artifactId>
			<version>1.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.jvnet.jaxb2_commons</groupId>
			<artifactId>jaxb2-basics-runtime</artifactId>
			<version>0.6.4</version>
		</dependency>
		<dependency>
			<groupId>org.jvnet.hyperjaxb3</groupId>
			<artifactId>maven-hyperjaxb3-plugin</artifactId>
			<version>0.5.6</version>
		</dependency>

	</dependencies>
</project>

Note: A great way to see the details for the plugin is to use the maven help command:

mvn help:describe -Dplugin=org.jvnet.hyperjaxb3:maven-hyperjaxb3-plugin -Ddetail

Running maven install now will generate the Java classes, extending the super-class, JPA annotations and all! Take a look inside the jaxb-from-pom_demo-0.0.1-SNAPSHOT.jar file.

References:

Example of a Shipment Odrer XSD (shiporder.xsd) from:
http://www.w3schools.com/schema/schema_example.asp

Hyperjaxb3:
http://confluence.highsource.org/display/HJ3/Home
http://confluence.highsource.org/display/HJ3/Purchase+Order+Tutorial

Leave a comment