简体   繁体   English

无法删除具有OneToMany关系的记录

[英]Cannot delete a record with OneToMany relation

When I delete a record in a table, related record should be deleted in another table. 当我删除表中的记录时,应在另一个表中删除相关记录。 But I receive instead: 但我接受了:

java.sql.BatchUpdateException: Batch entry 0 update child_table set parent_table_id=null where parent_table_id=63 was aborted java.sql.BatchUpdateException:批处理项0更新child_table set parent_table_id = null其中parent_table_id = 63已中止

The exception above is thrown against the following settings: 针对以下设置抛出上述异常:

@OneToMany(cascade = javax.persistence.CascadeType.ALL, targetEntity = ChildTable.class)
@JoinColumn(name = "parent_table_id")
@org.hibernate.annotations.Fetch(FetchMode.SUBSELECT)
public List<ChildTable> getTables() {
    return tables;
}

If I'm not mistaken, with such annotations, when a record is deleted in ParentTable, corresponding relation should be deleted in Child one. 如果我没有弄错,使用这样的注释,当在ParentTable中删除记录时,应该在Child 1中删除相应的关系。 It tries to become "null" (because corresponding record exists no more) before complete deletion. 它会在完全删除之前尝试变为“null”(因为相应的记录不再存在)。 This id column is "not null". 此id列为“not null”。 When I make it be "null" (just for testing this case) everything works correctly. 当我将其设为“null”(仅用于测试此案例)时,一切正常。

Could you help me understand what is the real problem behind this? 你能帮我理解这背后的真正问题是什么吗?

Thank you in advance. 先感谢您。

I usually don't use annotations. 我通常不使用注释。 I prefer hbm mapping files so I can tell you some possible solutions with hbm mappings. 我更喜欢hbm映射文件,所以我可以用hbm映射告诉你一些可能的解决方案。 :) I think you need to add inverse=true in your parent one-to-many relationship. :)我认为你需要在父母一对多关系中添加inverse = true。 Always put inverse=true in your parent collections . 始终在您的父集合中放置inverse = true For annotation equivalent of inverse=true please see this . 对于等于inverse = true的注释,请看这个

If this doesn't work, I know it would not be a good solution, you can give a try with not-found="ignore" or @NotFound(action=NotFoundAction.IGNORE) in the one-to-many relationship in parent. 如果这不起作用,我知道这不是一个好的解决方案,你可以尝试使用not-found =“ignore”或@NotFound(action = NotFoundAction.IGNORE)在父对话中的一对多关系。 Eventually, it has same effect as making not-null=false for the foreign key column in database. 最终,它与对数据库中的外键列使用not-null = false具有相同的效果。

One of the responders gave correct answer, but he deleted it. 其中一位回应者给出了正确答案,但他删除了它。 I had to use hibernate Cascade annotation instead of JPA one. 我不得不使用hibernate Cascade注释而不是JPA注释。 It didn't work for me because the problem was a little bit deeper than just usage of JPA and hibernate annotations together. 它对我不起作用,因为问题比仅使用JPA和hibernate注释要深一些。 Mapper class for ChildTable had parent_table_id of int type instead of a ParentTable class type the id field is mapped in. So I changed it from: ChildTable的Mapper类具有int类型的parent_table_id,而不是id字段被映射到的ParentTable类类型。所以我将其更改为:

@Entity
@Table(name = "child_table")
public class ChildTable {

private int id;
private int parent_table_id;
...

to

@Entity
@Table(name = "child_table")
public class ChildTable {

private int id;
private ParentTable parent_table;
...

Surely some additional changes (connected to this one) were made and deletion started to work as expected. 肯定会进行一些额外的更改(与此相关)并且删除开始按预期工作。

So, if you have corresponding error during deletion of an entity in one-to-many relation and everything is correct with annotations usage, check the types, your fields are of, in mapper-classes :) 因此,如果在删除一对多关系中的实体期间出现相应的错误,并且注释用法一切正确,请检查类型,您的字段是mapper-classes :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM