Implementing Object.equals(Object)
by natural keys
The last section's result actually provides a hint to
implement Object.equals(Object)
in a more
meaningful way. The problem comparing transient instances occurs
since a surrogate key's value is being provided by the database
server only at the time an entity is being persisted. If at least
one natural key (sometimes referred to as “business
key”) is being defined this one may be used instead:
Object.equals(Object)
by natural keys package session3;
@Entity
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"uid"})❶ ,
@UniqueConstraint(columnNames={"uidNumber"})})
public class User {
...
String uid;
@Column(nullable=false)
public String getUid() {return uid;}
public void setUid(String uid) {this.uid = uid;}
...
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof User) {
return getUid().equals(((User) other).getUid()); ❷
} else {
return false;
}
}
@Override
public int hashCode() {
if (null == getUid()) {
return System.identityHashCode(this);
} else {
return getUid().hashCode(); ❸
}
}
}
No. 24
Method and field access
Q: |
Consider Figure 947, “Implementing |
A: |
JPA allows lazy fetch mode
typically enabled by default. So the |