当通过saveAndFlush持久化/合并父实体时,它会尝试为子实体插入新记录,而不是查找/合并现有记录。这会导致SQLIntegrityConstraintViolationException
错误。我还尝试直接通过DAO拉取现有实体,将其设置为父实体中的字段,然后保存和刷新,它仍然尝试为子实体字段插入新记录。
非常感谢任何帮助!
儿童实体
@Entity
@Table(name = "DROPDOWN_TYPE", uniqueConstraints = {
@UniqueConstraint(columnNames = { "DROPDOWN_TYPE_TXT" }, name = "DROPDOWN_TYPE_TXT_UK") })
public class DropdownType {
@Id
@Column(name = "DROPDOWN_TYPE_TXT")
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DropdownType that = (DropdownType) o;
return text.equals(that.text);
}
@Override
public int hashCode() {
return text.hashCode();
}
}
家长实体
@Entity
@Table(name = "DROPDOWN", uniqueConstraints = {
@UniqueConstraint(columnNames = { "DROPDOWN_TXT", "DROPDOWN_TYPE_TXT" }, name = "DROPDOWN_UK") })
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Dropdown {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "DROPDOWN_OPTION_ID_GENERATOR")
@SequenceGenerator(allocationSize = 1, name = "DROPDOWN_OPTION_ID_GENERATOR", sequenceName = "DROPDOWN_OPTION_ID_SQ")
@Column(name = "DROPDOWN_OPTION_ID")
private Long id;
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST })
@JoinColumn(name = "DROPDOWN_TYPE_TXT", foreignKey = @ForeignKey(name = "DROPDOWN_TYPE_TXT_FK"))
private DropdownType dropdownType;
@Column(name = "DROPDOWN_TXT")
private String text;
@Column(name = "ACTIVE_FLG")
private Boolean active;
@Column(name = "LEGACY_FLG")
private Boolean legacy;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public DropdownType getDropdownType() {
return dropdownType;
}
public void setDropdownType(DropdownType dropdownType) {
this.dropdownType = dropdownType;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Boolean isActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
public Boolean isLegacy() {
return legacy;
}
public void setLegacy(Boolean legacy) {
this.legacy = legacy;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dropdown dropdown = (Dropdown) o;
return dropdownType.equals(dropdown.dropdownType) && text.equals(dropdown.text);
}
@Override
public int hashCode() {
int result = dropdownType != null ? dropdownType.hashCode() : 0;
result = 31 * result + (text != null ? text.hashCode() : 0);
return result;
}
}
答案 0 :(得分:0)
如果您使用hibernate作为JPA提供程序,请在覆盖equals和hashcode时小心 - see this post
可能是,您的JPA提供程序不认为您的实体是相同的,因为加载的实体实际上可能是某些CGLIB代理(可能更好地使用instanceof
而不是比较类)。