有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java保存实体尝试插入新记录,而不是与以前的记录合并

通过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;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    如果您使用hibernate作为JPA提供程序,那么在重写equals和hashcode see this post时要小心

    可能是,您的JPA提供者并不认为您的实体是相等的,因为加载的实体实际上可以是一些CGLIB代理(可能更好地使用^ {CD1>}而不是比较类)。p>