java - What should be included in equals and hashcode for JPA Entity -
in terms of creating jpa entity should , should not go equals , hashcode. example have address entity follows.
i've read id should not included, not sure why. nested objects state in case? did not include locations because state non owning end, location owns relationship.
of following class should , should not in equals , hashcode?
@entity @table(name = "t_address") @xmlrootelement @equalsandhashcode(exclude = {"id", "locations"}) @tostring(exclude = {"location"}) public class address implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue(strategy = generationtype.sequence, generator = "addressseq") @sequencegenerator(name = "addressseq", sequencename = "t_address_seq", allocationsize = 1) @column(name = "id") private long id; @size(max = 255) @column(name = "street_line_1") private string streetline1; @size(max = 255) @column(name = "street_line_2") private string streetline2; @notblank @size(max = 255) @column(name = "city") private string city; @manytoone(fetch = fetchtype.eager, optional = false) @joincolumn(name = "state_id", referencedcolumnname = "id") private state state; @notblank @size(max = 10) @column(name = "postal_code") private string postalcode; // referenced properties @onetomany(fetch = fetchtype.lazy, mappedby = "address") private list<location> locations; public address() { } public address(string streetline1, string streetline2, string city, state state, string postalcode) { this.streetline1 = streetline1; this.streetline2 = streetline2; this.city = city; this.state = state; this.postalcode = postalcode; } public long getid() { return id; } public void setid(long id) { this.id = id; } public string getstreetline1() { return streetline1; } public void setstreetline1(string streetline1) { this.streetline1 = streetline1; } public string getstreetline2() { return streetline2; } public void setstreetline2(string streetline2) { this.streetline2 = streetline2; } public string getcity() { return city; } public void setcity(string city) { this.city = city; } public state getstate() { return state; } public void setstate(state state) { this.state = state; } public string getpostalcode() { return postalcode; } public void setpostalcode(string postalcode) { this.postalcode = postalcode; } public list<location> getlocations() { return locations; } public void setlocations(list<location> locations) { this.locations = locations; } }
the recommendation have seen equality entities should reflect appropriate sense of equality business logic, opposed persistence logic. if using surrogate ids rather natural ids, normal jpa, means not base entities' equality on ids.
consider: suppose load address
entity database, , construct 1 based on data provided via application ui. if want there possibility test equals()
each other, must base test on properties other than id, because latter object not yet have id assigned. it's properties should contribute test.
of course, if ever override equals()
should override hashcode()
ensure invariant 2 objects test equals()
each other have same hash code. so, may use properties contribute equality test in hash code computation, , distinguishing hash code should use such properties.
note entity has association 1 or more other entities, ids of other entities may reasonably factor equality test. example, address
entity might include id of associated state
entity in equals()
, hashcode()
determinations.
Comments
Post a Comment