gpt4 book ai didi

java - JPA 实体的 equals 和 hashcode 中应该包含什么

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:30 26 4
gpt4 key购买 nike

<分区>

在创建 JPA 实体方面,什么应该和不应该进入 equals 和 hashcode。例如,我有一个地址实体,如下所示。

我读到不应包含 ID,但不确定原因。在我的例子中,像 State 这样的嵌套对象呢?我没有包括位置,因为状态是非拥有端,位置拥有关系。

在下面的类中,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;
}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com