【问题标题】:Hibernate does perform an update rather than a delete operation when a child entity gets removed当子实体被删除时,Hibernate 会执行更新而不是删除操作
【发布时间】:2015-12-02 21:11:46
【问题描述】:

我有两个实体,父(项目)有一个历史实体列表,当我从子列表(历史)item.getHistories().remove(0) 删除旧行时,我希望休眠执行 删除 对子行进行操作,但休眠将外键更新为null。 此更新导致 NOT NULL VIOLATION,因此外键具有 NOT NULL 约束。

这是我的父实体:

@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "item", schema = "public", catalog = "foo")
public class RepricingItem extends BaseDatesEntity {

    @Id
    @SequenceGenerator(name="item_id_seq", sequenceName = "item_id_seq", allocationSize = 5)
    @GeneratedValue(strategy = SEQUENCE, generator = "item_id_seq")
    @Column(name = "id", unique = true, insertable = true, updatable = false, nullable = false)
    protected Long id;

    @Size(min = 1, max = 100)
    @Column(name = "title", nullable = false, insertable = true, updatable = true)
    private String title;

    @OneToMany(fetch = LAZY, cascade = ALL, orphanRemoval = true)
    @JoinColumn(name = "item_id")
    private List<ItemHistory> histories;
}

这是孩子:

@Data
@EqualsAndHashCode(callSuper=true)
@Entity
@Table(name = "item_history", schema = "public", catalog = "foo")
public class RepricingItemBuyBoxHistory extends BaseEntity {

    @ManyToOne(fetch = LAZY, optional = false)
    @JoinColumn(name = "item_id", nullable = false, updatable = false)
    private RepricingItem repricingItem;

    @Size(min = 1, max = 255)
    @Column(name = "some_history_data", nullable = false, insertable = true, updatable = false)
    private String someHistoryData;
}

这是带有非空约束的历史表:

CREATE TABLE "item_history" (
    id                BIGSERIAL PRIMARY KEY,
    item_id           BIGINT                      NOT NULL REFERENCES "item" (id) ON DELETE CASCADE,
    inserted          TIMESTAMP WITHOUT TIME ZONE,
    updated           TIMESTAMP WITHOUT TIME ZONE,
    some_history_data VARCHAR(255)                NOT NULL
) WITH (OIDS =FALSE);

这是hibernate生成的更新子句:

11:17:00,794 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] SQL Error: 0, SQLState: 23502
11:17:00,794 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] Batch entry 0 update foo.public.item_history set item_id=null where item_id=903372 and id=245 was aborted.  Call getNextException to see the cause.

我如何告诉 hibernate 删除孩子而不是尝试删除关系。

【问题讨论】:

  • 在双向映射中,您必须处理关系的双方。
  • 删除机器人端没有帮助,休眠仍然发出更新命令:History entryToDelete = item.getHistories().get(0); repricingItem.getHistory().remove(entryToDelete); session.delete(entryToDelete); session.flush();
  • 我认为问题出在映射上
  • 我在这里解释了我的解决方案:stackoverflow.com/a/65994729/418599

标签: java hibernate jpa


【解决方案1】:

我认为您需要在 RepricingItem 中的 @OneToMany 注释中添加一个“mappedBy”属性。我认为是这样的:

@OneToMany(fetch = LAZY, cascade = ALL, orphanRemoval = true, mappedBy = "repricingItem")
@JoinColumn(name = "item_id")
private List<ItemHistory> histories;

问题与哪个实体是“所有者”有关。可以在这里找到比我能给出的更好的解释: inverse = “true” example and explanation

【讨论】:

    猜你喜欢
    • 2013-01-31
    • 2018-12-04
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多