简体   繁体   中英

JAXB objects - hashcode and equals

We have a huge java application that entirely works based on JAXB serialization.The middleware server does all db access and sends all the data Objects in JAXB objects and serializes to XML and sends the data to UI ( C#.Net).

Most of the times after the data is populated from db access into the JAXB java objects , I will have to some processing like "sort the collection of objects based on attribute" , find the avg , do some calculation on the list of objects in the collection etc.

My major problem is, JAXB objects don't have equals and hashcode. So what I am doing is moving all the data to some user defined Data objects where I have hashcode, equals, compareTo defined so I can do all operations in the collections and then copy to the JAXB objects. I think this is a extra overhead.

Questions:

1) does jaxb objects support equals /hashcode/ compareTo - can I specifiy these in schema?

2) Any other better alternatives?

Thanks.

unfortunately, jaxb does not provide this out of the box. you can use this plugin , or write your own for more customizable behavior.

It looks like you need to do use Collections.sort(list, Comparable) to accomplish the sorting that you want. Equals and hashcode won't help either of the cases you mentioned since your cases rely on comparison of specific attributes, not the object as a whole.

The other cases of finding averages and performing calculations also have nothing to do with equals/hashcode that I can see. These operations would simply require parsing the lists and performing your appropriate algorithm.

FWIW,虽然JAXB生成的Java类不具有equals和hashcode,但您可以在使用JAXB注释编写的类中添加这些覆盖 - JAXB将忽略这些方法。

maven-jaxb2-plugin can generate hashcode and equals method using its own plugin: org.jvnet.jaxb2_commons . More details about configuration can be found here .

The relevant parts of the pom.xml are as follows:

<project
  <!-- other configuration -->
  <dependencies>
    <!-- other dependencies -->
    <dependency>
      <groupId>org.jvnet.jaxb2_commons</groupId>
      <artifactId>jaxb2-basics</artifactId>
      <version>0.11.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.13.1</version>
        <executions>
          <execution>
            <id>generate</id>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <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.11.0</version>
            </plugin>
          </plugins>
          <schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory>
          <cleanPackageDirectories>true</cleanPackageDirectories>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM