gpt4 book ai didi

java - 所有者删除时违反参照完整性约束(OneToMany 单向)

转载 作者:行者123 更新时间:2023-12-01 06:18:12 27 4
gpt4 key购买 nike

我在删除单向 OneToMany 关系中的所有者对象时遇到问题。

相关的部分是我有三个对象:Acronym、Expansion 和 Tag。

  • Acronym 与 Expansion 具有单向 OneToMany 关系
  • 扩展与标签具有双向多对多关系

代码如下:

@Entity
public class Acronym extends Model {
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "acronym_id")
public Set<Expansion> expansions;
...
}

@Entity
public class Expansion extends Model {
@ManyToMany
public Set<Tag> tags;
...
}

@Entity
public class Tag extends Model {
@ManyToMany(mappedBy = "tags")
public Set<Expansion> expansions;
...
}

现在,当我尝试删除 Acronym 对象时,出现以下错误:

[error] Test models.AcronymTest.testAsJSON failed: javax.persistence.PersistenceException: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK_EXPANSION_TAG_EXPANSION_01: PUBLIC.EXPANSION_TAG FOREIGN KEY(EXPANSION_ID) REFERENCES PUBLIC.EXPANSION(ID) (7)"; SQL statement:
[error] delete from expansion where acronym_id = ? [23503-168]
[error] at com.avaje.ebeaninternal.server.persist.ExeUpdateSql.execute(ExeUpdateSql.java:76)
[error] at com.avaje.ebeaninternal.server.persist.DefaultPersistExecute.executeSqlUpdate(DefaultPersistExecute.java:125)
[error] at com.avaje.ebeaninternal.server.core.PersistRequestUpdateSql.executeNow(PersistRequestUpdateSql.java:44)
[error] at com.avaje.ebeaninternal.server.core.PersistRequest.executeStatement(PersistRequest.java:74)
[error] at com.avaje.ebeaninternal.server.core.PersistRequestUpdateSql.executeOrQueue(PersistRequestUpdateSql.java:49)
[error] at com.avaje.ebeaninternal.server.persist.DefaultPersister.executeSqlUpdate(DefaultPersister.java:147)
[error] at com.avaje.ebeaninternal.server.persist.DefaultPersister.deleteManyDetails(DefaultPersister.java:1169)
[error] at com.avaje.ebeaninternal.server.persist.DefaultPersister.deleteAssocMany(DefaultPersister.java:1143)
[error] at com.avaje.ebeaninternal.server.persist.DefaultPersister.delete(DefaultPersister.java:624)
[error] at com.avaje.ebeaninternal.server.persist.DefaultPersister.delete(DefaultPersister.java:452)
[error] at com.avaje.ebeaninternal.server.core.DefaultServer.delete(DefaultServer.java:1867)
[error] at com.avaje.ebeaninternal.server.core.DefaultServer.delete(DefaultServer.java:1857)
[error] at com.avaje.ebean.Ebean.delete(Ebean.java:648)
[error] at play.db.ebean.Model.delete(Model.java:167)
[error] at models.AcronymTest.testAsJSON(AcronymTest.java:201)
[error] ...
[error] Caused by: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK_EXPANSION_TAG_EXPANSION_01: PUBLIC.EXPANSION_TAG FOREIGN KEY(EXPANSION_ID) REFERENCES PUBLIC.EXPANSION(ID) (7)"; SQL statement:
[error] delete from expansion where acronym_id = ? [23503-168]
[error] at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
[error] at org.h2.message.DbException.get(DbException.java:169)
[error] at org.h2.message.DbException.get(DbException.java:146)
[error] at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:414)
[error] at org.h2.constraint.ConstraintReferential.checkRowRefTable(ConstraintReferential.java:431)
[error] at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:307)
[error] at org.h2.table.Table.fireConstraints(Table.java:871)
[error] at org.h2.table.Table.fireAfterRow(Table.java:888)
[error] at org.h2.command.dml.Delete.update(Delete.java:99)
[error] at org.h2.command.CommandContainer.update(CommandContainer.java:75)
[error] at org.h2.command.Command.executeUpdate(Command.java:230)
[error] at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:156)
[error] at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:142)
[error] at com.jolbox.bonecp.PreparedStatementHandle.executeUpdate(PreparedStatementHandle.java:203)
[error] at com.avaje.ebeaninternal.server.persist.ExeUpdateSql.execute(ExeUpdateSql.java:68)
[error] ... 48 more

我尝试通读 JPA 2.0 规范及其有关关系映射默认值的部分,并包含了我认为是给定对象关系的相关注释,但有些东西无法正常工作。该应用程序是基于 Java Play 构建的!框架(版本 2.1.3)仅供引用。

对我可能做错了什么有什么想法吗?

注意:
我正在寻找的功能是

  • 当您创建/更新/删除缩写词时,还应创建/更新/删除任何关联的扩展

  • 当您删除扩展或标签时,关联的标签或扩展不应分别受到影响(标签可以在没有关联扩展的情况下存在,反之亦然)

最佳答案

当您删除 Acronym 时,其关联的 Expansion 对象也会被删除。但是,在 Tag 表上,Expansion 表有一个外键,这会阻止 Expansion 被删除。

我建议您重新审视您的设计,并仔细考虑删除功能以及您是否真的希望删除首字母缩略词来删除所有相关的扩展和标签。这似乎不是一个现实的用例。

关于java - 所有者删除时违反参照完整性约束(OneToMany 单向),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18527774/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com