环境:
当我查询Moviment时,我得到了错误EntityNotFoundException,并且在我修改Espai类以使用注释@MappedSuperclass在单独的类中分隔id和版本后,我自动尝试加载相关的Espai。
我可以怀疑它是与equals()和hashCode()方法相关的东西,但我无法弄清楚问题。
Espai工作正常
@Entity
@Table(name = "a_espai")
public class Espai implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String nom;
private Integer version;
@ManyToOne
@JoinColumn(name = "idServei", referencedColumnName = "id")
private Servei servei;
...
@Override
public int hashCode() {
int hash = servei == null ? 1 : servei.getId();
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Espai)) {
return false;
}
Espai other = (Espai) obj;
return id != null && id.equals(other.getId());
}
}
更改后的Espai会引发错误
@Entity
@Table(name = "a_espai")
public class Espai extends BaseEntity implements Serializable {
private String nom;
@ManyToOne
@JoinColumn(name = "idServei", referencedColumnName = "id")
private Servei servei;
@Override
public int hashCode() {
int hash = 0;
if (servei == null) {
return hash;
} else {
return servei.getId();
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Espai)) {
return false;
}
Espai other = (Espai) obj;
return getId() != null && getId().equals(other.getId());
}
}
Espai在急切加载时遇到错误的移动课
@Entity
@Table(name = "t_moviment")
public class Moviment extends BaseEntity implements Serializable {
private LocalDate dataInici;
private LocalDate dataFi;
private String observacio;
@ManyToOne
@Basic(optional = false)
@JoinColumn(name = "idEspai", referencedColumnName = "id")
private Espai espai;
...
}
**Espai before change that provokes the error**
@MappedSuperclass
public class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Version
protected Integer version;
public Integer getId() {
return id;
}
}