JAXB equals and Hashcode: gives error

jeff porter picture jeff porter · May 30, 2013 · Viewed 12.5k times · Source

I'm trying to get my pom.xml to generate the hashCode() and equals methods() on my JAXB objects.

<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>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
    <dependency>
        <groupId>org.jvnet.jaxb2_commons</groupId>
        <artifactId>jaxb2-commons-lang</artifactId>
        <version>2.3</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.8.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <extension>true</extension>
                <args>
                    <arg>-XtoString</arg>
                    <arg>-Xequals</arg>
                    <arg>-XhashCode</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.6.2</version>
                    </plugin>
                </plugins>
            </configuration>
        </plugin>

    </plugins>
</build>

This gives the following errors though...

package org.jvnet.jaxb2_commons.lang does not exist
package org.jvnet.jaxb2_commons.locator does not exist
package org.jvnet.jaxb2_commons.locator.util does not exist
cannot find symbol symbol: class Equals
cannot find symbol symbol: class HashCode
cannot find symbol symbol: class ToString
cannot find symbol symbol  : class ObjectLocator
cannot find symbol symbol  : class ToStringStrategy
cannot find symbol symbol  : class HashCodeStrategy

I've googled about, and I've found a post about adding..

<dependency>
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>runtime</artifactId>
    <version>0.4.1</version>
</dependency>
<dependency>
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>testing</artifactId>
    <version>0.4.1</version>
    <scope>test</scope>
</dependency>

But this doesn't help.

Can anyone help me out please!

Answer

Tom Hargrave picture Tom Hargrave · Jun 6, 2013

Here is a configuration set that we have working. I believe there are a number of elements that need to "line up" (so to speak) otherwise you get the error indicated.

General Dependency:-

    <dependency>
        <groupId>org.jvnet.jaxb2_commons</groupId>
        <artifactId>jaxb2-basics-runtime</artifactId>
        <version>0.6.4</version>
    </dependency>

maven-jaxb2-plugin:-

        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.8.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <extension>true</extension>
                <generatePackage>you.package.etc</generatePackage>
                <args>
                    <arg>-XtoString</arg>
                    <arg>-Xequals</arg>
                    <arg>-XhashCode</arg>
                    <arg>-Xinheritance</arg>
                    <arg>-Xcopyable</arg>
                    <arg>-XenumValue</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.6.4</version>
                    </plugin>
                </plugins>
            </configuration>
        </plugin>

Bindings Extract:-

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
    jaxb:extensionBindingPrefixes="inheritance"
    jaxb:version="2.1">

......

<jaxb:bindings schemaLocation="yourschema.xsd" node="/xs:schema">
    <jaxb:bindings node="//xs:element[@name='element-you-want-to-have-interface']">
        <jaxb:class />
        <inheritance:implements>your.interface</inheritance:implements>
    </jaxb:bindings>
</jaxb:bindings>

......

</jaxb:bindings>