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

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

问题

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

  1. public class PlanItemDefinition {
  2. @Id
  3. @Column(name = "id")
  4. @GeneratedValue(generator = "uuid2")
  5. @GenericGenerator(name = "uuid2", strategy = "uuid2")
  6. private UUID id;
  7. ...
  8. @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
  9. @JoinColumn(name = "default_control_id", foreignKey = @ForeignKey(name = "fk_plan_item_definition_default_control_id"))
  10. private PlanItemControl defaultControl;
  11. ...
  12. }
  13. public class PlanItemControl {
  14. @Id
  15. @Column(name = "id")
  16. @GeneratedValue(generator = "uuid2")
  17. @GenericGenerator(name = "uuid2", strategy = "uuid2")
  18. private UUID id;
  19. ...
  20. @OneToOne(mappedBy = "defaultControl")
  21. private PlanItemDefinition planItemDefinition;
  22. ...
  23. }

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

  1. Caused by: java.sql.BatchUpdateException: Batch entry 1 insert into model.plan_item_definition
  2. (..., 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"
  3. 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.
  4. at org.postgresql.jdbc.BatchResultHandler.handleError(BatchResultHandler.java:169)
  5. at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2286)
  6. at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:521)
  7. at org.postgresql.jdbc.PgStatement.internalExecuteBatch(PgStatement.java:870)
  8. at org.postgresql.jdbc.PgStatement.executeBatch(PgStatement.java:893)
  9. at org.postgresql.jdbc.PgPreparedStatement.executeBatch(PgPreparedStatement.java:1639)
  10. at com.zaxxer.hikari.pool.ProxyStatement.executeBatch(ProxyStatement.java:128)
  11. at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeBatch(HikariProxyPreparedStatement.java)
  12. at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:118)
  13. ... 123 more
  14. 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"
  15. Detail: Key (default_control_id)=(9572c4fc-4a1b-4747-878e-43d8ec8b8fd6) is not present in table "plan_item_control".
  16. at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2553)
  17. at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2285)
  18. ... 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 ?

暂无答案!

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

相关问题