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

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.11.1</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.


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.


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>