在创建JPA实体时,equals和hashcode应该包含什么,不应该包含什么?以Address实体为例。
我读到过ID不应该包含在内,但不确定为什么。像我的情况中的State这样的嵌套对象呢?我没有包含locations,因为State是非拥有端,Location拥有关系。
以下类中应该包含什么,不应该包含什么?
@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;
}
}