entity.equals doesn't work correct with proxies -------------------------------------------------
Key: HIB-138 URL: http://jira.andromda.org/browse/HIB-138 Project: Hibernate Cartridge Type: Bug Versions: 3.1M1 Reporter: Patrik Jurica Assigned to: Carlos Cuenca Priority: Minor Generated Java code for entity.equals(Object object) doesn't work, when the equaled paramater is a CGLIB proxy, beacuse "that.$attribute.name" is used. But "that.$attribute.name" can be null in the proxy object. "that.${attribute.getterName}()" must be used instead. The corrected templates\hibernate\hibernate.java.vm follows: ## ## This macro will render the equals() method ## If an entity is rendered, the attributeSet consists of the identifiers, ## if an value type is renderer, the attributeSet consists of all attributes ## #macro (renderEqualsMethod $class $className $attributeSet) public boolean equals(Object object) { #if ($attributeSet.empty) return super.equals(object); #else if (this == object) { return true; } if (!(object instanceof $className)) { return false; } final $className that = ($className)object; #foreach ($attribute in $attributeSet) #set ($idType = $attribute.type) #if ($idType.primitive) if (this.$attribute.name != that.${attribute.getterName}()) { return false; } #elseif ($idType.arrayType) if (!java.util.Arrays.equals(this.$attribute.name, that.${attribute.getterName}())) { return false; } #else if (this.$attribute.name == null || that.${attribute.getterName}() == null || !this.${attribute.name}.equals(that.${attribute.getterName}())) { return false; } #end #end return true; #end } #end ## ## This macro will render the hashCode() method ## If an entity is rendered, the attributeSet consists of the identifiers, ## if an value type is renderer, the attributeSet consists of all attributes ## #macro (renderHashCodeMethod $class $attributeSet) public int hashCode() { #if ($attributeSet.empty) return super.hashCode(); #else #if ($class.generalization) int hashCode = super.hashCode(); #else int hashCode = 0; #end #foreach ($attribute in $attributeSet) #set ($attrType = $attribute.type) #if ($attribute.getterSetterTypeName == "boolean") hashCode = 29 * hashCode + (${attribute.name} ? 1 : 0); #elseif ($attrType.arrayType) // arrays are not part of the hashCode calculation #elseif ($attrType.primitive) hashCode = 29 * hashCode + (int)${attribute.name}; #else hashCode = 29 * hashCode + (${attribute.name} == null ? 0 : ${attribute.name}.hashCode()); #end## if #end## foreach return hashCode; #end## $attributeSet.empty } #end ------------------------------------------------------- This SF.Net email is sponsored by: Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl