Generating hashCode() and equals() when creating Java classes using Mojo Jaxb2 maven plugin

17.3k views Asked by At

The code I'm working on is using jaxb2-maven-plugin from org.codehaus.mojo to generate Java classes from XSD schema. I'm looking for a way to automatically implement equals() and hashCode() methods for those classes, but it seems there is not a way. I know that there are other JAXB2 Maven plugins that do that (http://confluence.highsource.org/display/J2B/Home for example), but I was wondering if anyone of you encountered this issue before and if there's a way for fixing it. I'm generating the classes using the xjc goal.

7

There are 7 answers

9
lexicore On BEST ANSWER

JAXB2 Basics you're mentioning is not a property of maven-jaxb2-plugin, it is a standalone set of JAXB 2.x plugins you can use with XJC - or jaxb2-maven-plugin or whatever.

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.3.1</version>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
           </executions>
           <configuration>
                <arguments>
                     <argument>-Xequals</argument>
                     <argument>-XhashCode</argument>
                </arguments>
           </configuration>
           <dependencies>
                <dependency>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics</artifactId>
                    <version>0.12.0</version>
                </dependency>
           </dependencies>
       </plugin>

What I wanted to ask - why not just use maven-jaxb2-plugin? It has so much more functionality compared to the Codehaus plugin - including configuration support for JAXB2 plugins.

1
boumbh On

My answer is for those who can't afford any third part dependency on their generated code.

The org.jvnet.jaxb2_commons:jaxb2-basics plugin adds a lots of org.jvnet includes in the generated code and your project will depend on org.jvnet.jaxb2_commons:jaxb2-basics-runtime.

The org.andromda.thirdparty.jaxb2_commons:commons-lang-plugin plugin generates code which depends on commons-lang:commons-lang. This dependency might be easier to bear depending on the situation.

I finally found this source code, it might be included in com.sun.xml.bind:jaxb-xjc at some point (version 2.2.4 ?), but until then I can't see any other solution than writing your own plugin.

PS: I can't access confluence.highsource.org, I get a 404. I guess it might have been helpful.

PPS: In my situation, the application I am building is targeted to an environment which has a very limited set of allowed Java libraries (incongruous security restrictions). I was a bit disappointed by the fact that jaxb2_commons includes a bunch of org.jvnet dependencies in the generated source, just for adding a boring equals method. I can understand the strategy of jvnet, their tools are very powerful, I may just be trying and using a sledgehammer to crack a nut. Still, I was sorry to note that it was so difficult to find a suitable tool for my situation.

3
thatidiotguy On

I would strongly disagree with using JAXB generated classes as business objects in your code. The classes that are generated by JAXB are beans that are just meant to essentially move element information from the xml file, to the bean's fields. I personally always have my SOAP service convert these generated bean classes to my actual business objects, as XML->Java and vice versa conversion is not black and white all the time. Note that this is my personal opinion, and would love for some others to weigh in on what they do in this situation.

To answer your question though, use a different plug in, or just your use your IDE to make some equals and hashCode methods.

Hope that helps.

EDIT:

I forgot to put my reasoning for this, apologies. Let us say in the next version of your project you want to add some more behavior to your JAXB generated classes, but also want to make some changes to your schema. Now you are regenerating the JAXB generated classes, putting the old behaviors back in, and making your application far more susceptible to bugs in my opinion. The JAXB generated classes are supposed to reflect your XML schema types (and thus your SOAP messages) not your business logic. Hope that makes sense.

0
Philipp On

You might also want to consider using a fluent builder interface (facilitates the handling of the generated JAXB classes) and a reference to a catalog file in case you are referencing an xmldsig-core-schema (speeds up the generation process, as no remote Schemas from W3C are queried - their servers usually delay the response).

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.5</version>
            <dependencies>
                <dependency>
                    <groupId>net.java.dev.jaxb2-commons</groupId>
                    <artifactId>jaxb-fluent-api</artifactId>
                    <version>2.0.1</version>
                    <exclusions>
                        <exclusion>
                            <groupId>com.sun.xml</groupId>
                            <artifactId>jaxb-xjc</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <dependency>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics</artifactId>
                    <version>0.6.4</version>
                    <exclusions>
                        <exclusion>
                            <groupId>com.sun.xml</groupId>
                            <artifactId>jaxb-xjc</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory>
                <packageName>[your package name goes here]</packageName>
                <outputDirectory>${build.directory}/generated/src/main/java</outputDirectory>
                <bindingDirectory>${basedir}/src/main/resources/xsd</bindingDirectory>
                <extension>true</extension>
                <catalog>${basedir}/src/main/resources/xsd-catalog/catalog.cat</catalog>
                <extension>true</extension>
                <arguments>-Xfluent-api -Xequals -XhashCode -XtoString</arguments>
            </configuration>
        </plugin>

Here is how the catalog file looks like:

--
  sample catalog file.

  double hyphens are used to begin and end a comment section.
--

SYSTEM "http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd" "xmldsig-core-schema.xsd"

And here is the link to the xmldisg-core-schema.xsd: http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd

Please note that the link is not broken - the W3C server is just delaying the response a few seconds. Can me very cumbersome if that happens during your automated build process, which includes JAXB generation.

0
Atish Narlawar On

This is the easiest way to do. Please update version as per your requirements.

<plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>jaxb2-maven-plugin</artifactId>
                    <version>1.5</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-commons-lang</artifactId>
                            <version>2.3</version>
                        </dependency>
                    </dependencies>
                    <executions>
                        <execution>
                            <id>JAXB generate content classes</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>xjc</goal>
                            </goals>
                            <configuration>
                                <schemaDirectory>${project.basedir}/src/main/resources/schema</schemaDirectory>
                                <outputDirectory>${project.build.directory}/generated-sources/jaxb</outputDirectory>
                                <schemaFiles>**/*.xsd</schemaFiles>
                                <packageName>com.lexus.web.content.model</packageName>
                                <arguments>-Xcommons-lang</arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
0
Mirko Klemm On

There is also a fluent-builder plugin for JAXB that doesn't necessarily generate dependencies to third-party code. Plus, it is a true "Builder" design pattern, whereas the "fluent-api" plugin just adds builder-like methods to the generated classes. It is: https://github.com/mklemm/jaxb2-rich-contract-plugin It also has options to make the generated classes immutable and it can generate methods to copy the state of existing objects into a new builder instance, also supporting "partial" object copy.

0
Ermal On

For me the simplest way to do is using maven-jaxb2-plugin (as mentioned in the accepted answer):

  1. Add in pom.xml <dependencies>
<dependency> 
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>jaxb2-basics</artifactId>
    <version>0.12.0</version>
</dependency>
  1. Add the plugin
<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.14.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <schemaDirectory>src/main/resources</schemaDirectory>
                <generateDirectory>target/generated-sources</generateDirectory>
                <generatePackage>my.package</generatePackage>
            </configuration>
        </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.12.0</version>
            </plugin>
        </plugins>
    </configuration>
</plugin>

See