简体   繁体   English

如何告诉Hibernate通过其外键指向的实体删除表行?

[英]How can I tell Hibernate to delete a table row through the entity it's foreign key points to?

I have three tables in my database; 我的数据库中有三个表。 an Author table, which has an id column, a Publisher table, which has an id column, and a Book table, which has an id column, an authorId foreign key column, to the Author id, and a publisherId foreign key column,to the Publisher id. 一个有一个id列的Author表,一个有一个id列的Publisher表,一个有一个id列,一个AuthorId的authorId外键列和一个PublisherId外键列的Book表,发布者ID。 The foreign key on update/on delete options are all set to 'NO ACTION', but from what I understand, Hibernate should be able to handle these itself. update / ondelete选项上的外键都设置为“ NO ACTION”,但是据我了解,Hibernate应该能够自己处理这些外键。

The Author entity has a set of Books, which contain the publisher Id, but do not know about the Author. Author实体具有一组Books,其中包含发布者ID,但不知道Author。 I've set this up as follows: 我将其设置如下:

@Entity
@Table(name="author")
public class Author {
    private Integer id;
    private Set<Book> books;

    @Id @GeneratedValue
    public Integer getId() {
        return id;
    }

    @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL) {
    @JoinColumn(name="authorId")
    public Set<Book> getBooks() {
        return books;
    }

    public void setBooks(Set<Book> books) {
        this.books = books;
    }
}

@Entity
@Table(name="book") {
    private Integer id;
    private Integer publisherId;

    @Id @GeneratedValue
    public Integer getId() {
        return id;
    }

    @ManyToOne(FetchType.EAGER)
    @JoinColumn(name="publisherId")
    public Integer getPublisherId() {
        return publisherId;
    }

    public void setPublisherId(Integer publisherId) {
        this.publisherId = publisherId;
    }
}

My problem is that when I attempt to delete a Book from the Author's set of books, Hibernate tries to update the authorId foreign key to null, and I get the error that the column authorId cannot be null in the Book table. 我的问题是,当我尝试从“作者”的书集中删除一本书时,Hibernate尝试将authorId外键更新为null,并且我得到一个错误,即Book表中的authorId列不能为null。 What I want to happen is for that row to be deleted entirely. 我想要发生的是将该行完全删除。

I have also tried to add 'orphanRemoval=true' to the OneToMany annotation, and set on update/on delete to Cascade, but I get the same error. 我还尝试将'orphanRemoval = true'添加到OneToMany批注中,并设置为在更新时/在删除时为Cascade,但我遇到相同的错误。

Which annotations can I use to indicate that when a book is removed from the Author's set of books, I want that row in the Book table to be removed? 我可以使用哪些注释来表示,当从“作者”的书集中删除一本书时,我希望“书”表中的该行被删除吗? Is this possible at all with how I have my relationships set up and, if not, what is a better way to approach this? 建立我的人际关系完全有可能吗?如果没有,建立这种关系的更好方法是什么?

In the database is the AuthorId column set to NOT NULL? 在数据库中,AuthorId列是否设置为NOT NULL? It could be that Hibernate is going to delete the row in two passes. Hibernate可能要分两次删除该行。 The first pass nulls the foreign key reference, the second pass then deletes the row. 第一次通过使外键引用为空,然后第二次通过删除行。 I'm using NHibernate and have certainly seen similar behaviour. 我正在使用NHibernate,并且肯定看到过类似的行为。 If this is the case, then you should be able to sort your problem out by setting the AuthorId to be NULLable. 如果是这种情况,那么您应该可以通过将AuthorId设置为NULLable来解决问题。

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

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