Reputation: 663
Lets say I'm writing a JPA entity. Very simple one. It has 3 properties:
public class MenuItem {
@Id
@GeneratedValue
private Long menuItemId;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private Type type;
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "colorId")
private Color color;
}
Now, lets say I need to override its equals
/hashcode
methods. According to this suggestion Implementing equals() and hashCode() I have to use some sort of "Business key equality". But I have doubts, what should I consider as a "Business" key here. Does it have to be all the properties except of ID
, or Color
can be excluded from it. Just seems unclear to me. Would be thankful if anyone can explain more on this topic.
Upvotes: 3
Views: 1307
Reputation: 26572
The last statement in the reference is pure gold:
Note that a business key does not have to be as solid as a database primary key candidate. Immutable or unique properties are usually good candidates for a business key.
So in your case name
and type
would be good candidates. Assuming the Type would be an immutable object (you could still get away without that but you might experience some hard to detect bugs).
IF you would put a unique constraint on color
then i would consider that in the equals
/ hashCode
method also.
Upvotes: 3