对所有者删除的参照完整性约束违规(OneToMany单向)

时间:2013-08-30 07:55:13

标签: java jpa playframework playframework-2.1 ebean

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

相关位是我有三个对象:缩写,扩展和标记。

  • 首字母缩略词与Expansion具有单向OneToMany关系
  • 扩展与Tag
  • 具有双向ManyToMany关系

以下是代码:

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

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

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

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

  • 删除扩展或标记时,不应分别关联相关标记或扩展(标记可以不存在关联的扩展,反之亦然)

1 个答案:

答案 0 :(得分:-1)

删除首字母缩略词时,会删除与之关联的扩展对象。但是,在Tag表上,有一个扩展表的外键,可以防止扩展被删除。

我建议您重新访问您的设计并仔细考虑删除功能以及是否确实要删除Acronym以删除所有相关的扩展和标记。这似乎不是一个现实的用例。