Hibernate version: 3.0.5
Hibernate tools version: Eclipse plugin 3.0.0 alpha4
Hi,
I generate the equals and hashCode methods using the meta-syntax on this hbm file:
Code:
<?xml version="1.0" encoding="ASCII"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-access="property" default-cascade="none" default-lazy="true" package="com.foo.bar.test.equals">
<class abstract="false" dynamic-insert="false" dynamic-update="false" mutable="true" name="TestEquals" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="testEquals">
<meta attribute="implement-equals" inherit="true">true</meta>
<id column="id" name="id" type="long">
<meta attribute="use-in-equals" inherit="true">true</meta>
<generator class="native"/>
</id>
<property column="text1" name="text1" type="string"/>
<property column="text2" name="text2" type="string"/>
<property column="text3" name="text3" type="string"/>
</class>
</hibernate-mapping>
obtaining this generated class:
Code:
/**
* TestEquals generated by hbm2java
*/
public class TestEquals implements java.io.Serializable {
// Fields
private Long id;
private String text1;
private String text2;
private String text3;
// Constructors
/** default constructor */
public TestEquals() {
}
/** constructor with id */
public TestEquals(Long id) {
this.id = id;
}
// Property accessors
/**
*
*/
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
/**
*
*/
public String getText1() {
return this.text1;
}
public void setText1(String text1) {
this.text1 = text1;
}
/**
*
*/
public String getText2() {
return this.text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
/**
*
*/
public String getText3() {
return this.text3;
}
public void setText3(String text3) {
this.text3 = text3;
}
public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( (other == null ) ) return false;
if ( !(other instanceof TestEquals) ) return false;
TestEquals castOther = ( TestEquals ) other;
return (this.getId()==castOther.getId()) || ( this.getId()!=null && castOther.getId()!=null && this.getId().equals(castOther.getId()) );
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getId().hashCode();
;
;
;
return result;
}
}
In the generated code for the method hashCode() there is a list of ";" as long as the number of properties of the class not included in the clause "use-in-equals". This is only a bit ridiculous to see, but should be fixed.
My concern is: is it possible to get a NullPointerException in the method hashCode()? What if I issue a new TestEquals()? In this case getId() should return null. Have I to define an explicit unsaved-value clause in the id mapping with a not null value?
Thanks
Adriano