0

My entity classes use the @Id or the @IdClass (for composite keys) annotation for comparison on JPA level. But now I want the entity classes itself to be comparable via an equals method.

My idea is to autogenerate those equals-methods and use them for testing, whether all values are set correctly in the entity file (against a manually generated one). In the productive scenario only a comparison of the keys is desired

Is there any interference between the @Id/@IdClass annotation for comparing tables on JPA site and my equals approach for testing?

Note: This equals method is a different one from the method I created in the composite key class of the @IdClass annotation.

Thank you

Felix
  • 95
  • 2
  • 11
  • 1
    I'm not quite sure what your question is after: EclipseLInk doesn't use your objects equals methods so you shouldn't have issues with the annotation, but I'd be careful in what you do, as many applications run into problems overriding equals and hashcode functions when using sets and maps. – Chris Oct 18 '17 at 14:05
  • You basically want to check if the data of the object is equal? You could define a new method for this like "dataIsEqual" or "contentIsEqual" (maybe in conjunction with an interface). So you don't have to risk a wrong implementation of the equals/hashCode method. – Mitch Oct 18 '17 at 14:18
  • @Chris thankyou. that is interesting. – Felix Oct 19 '17 at 06:20
  • @Mitch yeah I could create a compare method. But equals already has autogenerate in my IDE and I am dealing with 140 different classes. Maybe I will just create the equals and rename them to compare. – Felix Oct 19 '17 at 06:21
  • Maybe another idea: In our last project with JPA/EclipseLink we implemented the Entity-Classes equals/hashCode methods in this way: First they considered the id, but if both objects had no id, the content was checked for equality. But this still might be an issue with Collections, as @Chris already wrote. This behaviour might also be autogenerated using the EqualsBuilder from Apache Commons. (https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/builder/EqualsBuilder.html) – Mitch Oct 19 '17 at 06:32
  • Oh and also this thread is about a similar problem: (https://stackoverflow.com/questions/5031614/the-jpa-hashcode-equals-dilemma?rq=1) – Mitch Oct 19 '17 at 06:37

0 Answers0