CRITICAL
Rule Definition
Since the programmer can define the meaning of Java Equality, it is important not to use the id field in this definition if the id field is a surrogate key. This is because Hibernate only sets the field when saving the object. Hence, for example, if you add the object to some set collection, then saving the object will result in its identity changing, and part of the rules about using Set/Map collection class is that the contained object's identity must not change while it is in the collection (the behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element).
For example:
Person p = new Person();
Set set = new HashSet();
set.add(p);
System.out.println(set.contains(p));
p.setId(new Long(5));
System.out.println(set.contains(p));
==> Prints: false
As the behavior of a Set/Map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the Set or is the key of a Map., you may corrupt you database.
Remediation
Implement your equals() and hashCode() methods based on business keys. In cases where the business key is composite, you can use the Jakarta Commons libraries - in particular the EqualsBuilder and HashCodeBuilder classes. It may be that your class is not an @Entity, but a value object (@Embeddable) instead and perhaps the entity needs to be modelled in a different way.
Violation Code Sample
------> sample.hbm.xml:
...
<class name="Sample" table="SAMPLE">
<id name="id" column="id" type="long">
<generator class="sequence"/>
</id>
<property name="name" column="NAME" type="string"/>
...
</class>
------> Sample.java:
public class Sample {
private long id;
private String name;
...
public String getName() {
return name;
}
public Long getId() {
return id;
}
public boolean equals(Object other) {
if (this==other) return true;
if ( !(other instanceof Sample) ) return false;
final Sample that = (Sample) other;
return this.getId().equals( that.getId()); // VIOLATION
}
}
Fixed Code Sample
------> Sample.java:
public class Sample {
private long id;
private String name;
...
public String getName() {
return name;
}
public Long getId() {
return id;
}
public boolean equals(Object other) {
if (this==other) return true;
if ( !(other instanceof Sample) ) return false;
final Sample that = (Sample) other;
return this.getName().equals( that.getName()); // FIXED
}
}
Reference
Hibernate in Action (ISBN 1932394-15-X) p 123, Java Persistence with Hibernate (ISBN 1-932394-88-5) 397, http://www.cs.bham.ac.uk/~aps/syllabi/2004_2005/issws/h03/hibernate.html#object-lifecycle, http://techpolesen.blogspot.com/2007/11/10-tips-on-jpa-domain-modelling.html, http://java.sun.com/j2se/1.4.2/docs/api/java/util/Set.html, http://www.ideyatech.com/2011/04/effective-java-equals-and-hashcode/
Related Technologies
JEE
Technical Criterion
PCI-DSS4-Requirement-6.2.4 - Software engineering techniques or other methods are defined and in use by software development personnel to prevent or mitigate common software attacks and related vulnerabilities
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.