Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB objects - hashcode and equals

Tags:

java

jaxb

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.

like image 668
java_mouse Avatar asked Oct 14 '11 18:10

java_mouse


People also ask

What is the hashCode () and equals () used for?

Java hashCode() Java Object hashCode() is a native method and returns the integer hash code value of the object. The general contract of hashCode() method is: Multiple invocations of hashCode() should return the same integer value, unless the object property is modified that is being used in the equals() method.

Can two equal objects have same hashCode?

1) If two objects are equal (i.e. the equals() method returns true), they must have the same hashcode. 2) If the hashCode() method is called multiple times on the same object, it must return the same result every time. 3) Two different objects can have the same hash code.

What is the difference between hashCode and equals?

The key difference between equals and hashCode in Java is that the equals is used to compare two objects while the hashCode is used in hashing to decide which group an object should be categorized into.

What are equals () and hashCode () overriding rules?

"If two objects are equal using Object class equals method, then the hashcode method should give the same value for these two objects." So, if in our class we override equals() we should override hashcode() method also to follow this rule.


3 Answers

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

like image 155
jtahlborn Avatar answered Nov 14 '22 10:11

jtahlborn


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.

like image 27
Robin Avatar answered Nov 14 '22 08:11

Robin


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.

like image 36
Paul Jackson Avatar answered Nov 14 '22 09:11

Paul Jackson