Tuesday, May 8, 2012

Bugs in the code which automatically generated by MyEclipse

Bugs in the code which automatically generated by MyEclipse.

The implementation of equals method to compare two Integers used by automatically generated code is integer1 == integer2, and this is wrong.

Integer is boxed type for primitive type int and Integers are objects, so logical equality check method equals should be used rather than identically comparison ==.

example.


import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
 * AssetassetId entity. @author MyEclipse Persistence Tools
 */
@Embeddable
public class AssetassetId implements java.io.Serializable {

// Fields

private Integer assetid1;
private Integer assetid2;

// Constructors

/** default constructor */
public AssetassetId() {
}

/** full constructor */
public AssetassetId(Integer assetid1, Integer assetid2) {
this.assetid1 = assetid1;
this.assetid2 = assetid2;
}

// Property accessors

@Column(name = "assetid1", nullable = false)
public Integer getAssetid1() {
return this.assetid1;
}

public void setAssetid1(Integer assetid1) {
this.assetid1 = assetid1;
}

@Column(name = "assetid2", nullable = false)
public Integer getAssetid2() {
return this.assetid2;
}

public void setAssetid2(Integer assetid2) {
this.assetid2 = assetid2;
}

public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof AssetassetId))
return false;
AssetassetId castOther = (AssetassetId) other;

return ((this.getAssetid1().equals(castOther.getAssetid1())) || (this
.getAssetid1() != null
&& castOther.getAssetid1() != null && this.getAssetid1()
.equals(castOther.getAssetid1())))
&& ((this.getAssetid2().equals(castOther.getAssetid2())) || (this
.getAssetid2() != null
&& castOther.getAssetid2() != null && this
.getAssetid2().equals(castOther.getAssetid2())));
}

public int hashCode() {
int result = 17;

result = 37 * result
+ (getAssetid1() == null ? 0 : this.getAssetid1().hashCode());
result = 37 * result
+ (getAssetid2() == null ? 0 : this.getAssetid2().hashCode());
return result;
}

}

No comments:

Post a Comment