外键冲突,order\u inserts=true,批处理中包含混合子类实体

fivyi3re  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(252)

问题

当批处理被启用时,hibernate以错误的顺序插入实体 hibernate.order_inserts=true . 这将导致外键冲突。我们有一个事务,它创建了一种包含许多不同实体的模型。所有内容都通过级联保存,包括提供外键冲突的以下实体:

public class PlanItemDefinition {

    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    private UUID id;

    ...

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "default_control_id", foreignKey = @ForeignKey(name = "fk_plan_item_definition_default_control_id"))
    private PlanItemControl defaultControl;

    ...

}

public class PlanItemControl {

    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    private UUID id;

    ...

    @OneToOne(mappedBy = "defaultControl")
    private PlanItemDefinition planItemDefinition;

    ...

}

当事务提交时,hibernate引发以下异常:

Caused by: java.sql.BatchUpdateException: Batch entry 1 insert into model.plan_item_definition 
(..., default_control_id, ...) values (..., '9572c4fc-4a1b-4747-878e-43d8ec8b8fd6'::uuid, ...) was aborted: ERROR: insert or update on table "plan_item_definition" violates foreign key constraint "fk_plan_item_definition_default_control_id"
  Detail: Key (default_control_id)=(9572c4fc-4a1b-4747-878e-43d8ec8b8fd6) is not present in table "plan_item_control".  Call getNextException to see other errors in the batch.
    at org.postgresql.jdbc.BatchResultHandler.handleError(BatchResultHandler.java:169)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2286)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:521)
    at org.postgresql.jdbc.PgStatement.internalExecuteBatch(PgStatement.java:870)
    at org.postgresql.jdbc.PgStatement.executeBatch(PgStatement.java:893)
    at org.postgresql.jdbc.PgPreparedStatement.executeBatch(PgPreparedStatement.java:1639)
    at com.zaxxer.hikari.pool.ProxyStatement.executeBatch(ProxyStatement.java:128)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeBatch(HikariProxyPreparedStatement.java)
    at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:118)
    ... 123 more
Caused by: org.postgresql.util.PSQLException: ERROR: insert or update on table "plan_item_definition" violates foreign key constraint "fk_plan_item_definition_default_control_id"
  Detail: Key (default_control_id)=(9572c4fc-4a1b-4747-878e-43d8ec8b8fd6) is not present in table "plan_item_control".
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2553)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2285)
    ... 130 more

当我移除 hibernate.order_inserts=true 一切正常。这似乎是同一个问题https://hibernate.atlassian.net/browse/hhh-9864?attachmentsortby=datetime 但是既然我们使用的是hibernate 5.2.17.final版本,这个问题应该得到解决吗?

问题

有人能告诉我这是一个冬眠虫还是我们做错了什么吗?hibernate不考虑insert语句在级联时的顺序吗 hibernate.order_inserts=true ?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题