1

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

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

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

这是代码:

@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 版)仅供参考。

关于我可能做错了什么的任何想法?

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

  • 当您创建/更新/删除首字母缩略词时,还应创建/更新/删除任何相关的扩展

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

4

1 回答 1

-1

当您删除首字母缩略词时,它关联的扩展对象将被删除。但是,在 Tag 表上,Expansion 表有一个外键,可防止该 Expansion 被删除。

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

于 2013-08-30T11:10:48.783 回答