sql - Java equals() and hashcode() during automatic code generation -
i working on project code gets automatically generated based upon mysql library. jpa, not quite.
this example bean:
public class templatebean implements bean { private integer templateid; private integer businesspartnerid; public templatebean(final integer businesspartnerid) { this.businesspartnerid = businesspartnerid; } private templatebean(final object nullobject, final integer templateid, final integer businesspartnerid) { this.templateid = templateid; this.businesspartnerid = businesspartnerid; } public templatebean(final resultset rs) throws sqlexception { this(null, rs.getint(1), rs.getint(2)); } public integer gettemplateid() { return templateid; } public void settemplateid(final integer templateid) { this.templateid = templateid; } public integer getbusinesspartnerid() { return businesspartnerid; } public void setbusinesspartnerid(final integer businesspartnerid) { this.businesspartnerid = businesspartnerid; } @override public string tostring() { return "template(" + templateid + ", " + businesspartnerid + ")"; } }
now need implement equals()
, hashcode()
. of course have access data available sql, think implementing equals()
should doable, how going create hashcode()
?
any tips appreciated.
i suggest use equalsbuilder
import org.apache.commons.lang.builder.equalsbuilder; import org.apache.commons.lang.builder.hashcodebuilder; public class person { private string id; private string name; private string address; private string phone; private string version; @override public boolean equals(object object) { return equalsbuilder.reflectionequals(this, object,); } @override public int hashcode() { return hashcodebuilder.reflectionhashcode(this); } or /* * equal() method exclude fields. * neglect id , version fields. * * */ @override public boolean equals(object object) { return equalsbuilder.reflectionequals(this, object, "id", "version"); } }
Comments
Post a Comment