开发者

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/ comp开发者_开发知识库areTo - 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, while the JAXB-generated Java classes will not have equals and hashcode, you can add these overrides in the classes you write with JAXB annotations - JAXB will ignore the methods.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜