使用hibernate删除没有书籍的出版商

发布于 2024-11-25 16:44:25 字数 1062 浏览 7 评论 0原文

在编写书店应用程序时,我需要为 Book 和 Publisher 类放置映射。

Book 与 Publisher 具有 n-to-1 关系。与 Author 也具有 n-to-1 关系。 我想给管理员用户一个删除书籍、作者或出版商的选项。

没有书籍的出版商没有任何意义。作者的情况也是如此。如果删除出版商的所有书籍,则必须从数据库中删除该出版商。 如果某个作者的所有书籍都被删除,则该作者也应该被删除。 听起来像是delete-orphan级联。但是多对一映射不支持delete-orphan。

另外,如果我删除一个出版商,他的所有书籍都应该被删除。 同样,删除作者应该删除他的所有书籍。

目前,我的 Author、Publisher 和 Book 类定义如下, 这本书有字段“作者”和“出版商”。但是,“作者”或“出版商”没有引用他们的书籍。 我必须重新定义这些类吗? 我应该如何映射图书和出版商之间的关系?

任何帮助表示赞赏

public class Book {
    private Long bookId;    
    private Author author;
    private Publisher publisher;
...
}

public class Publisher {
    private Long publisherId;   
    private String name;        
}

public class Author {
    private Long authorId;
    private String name;    
}

<class name="Book" table="BOOKS">
    <id name="bookId" column="BOOKID" type="long">
    <generator class="native"/>
    </id>
    ...
    <many-to-one name="publisher" class="Publisher"   column="PUBLISHERID" lazy="false" cascade="save-update"/>
  ...

while writing a bookstore application ,I needed to put mappings for Book and Publisher classes.

Book has n-to-1 relationship with Publisher.Also n-to-1 with Author .
I wanted to give the admin user an option to delete books,authors or publishers.

A Publisher with no Books makes no sense.Similar is the case for Author.If all books of a publisher are deleted,the publisher must be removed from db.
If all Books of an Author get deleted ,the Author should also be deleted.
It sounds like a delete-orphan cascading.But many-to-one mapping does not support delete-orphan .

Also ,if I delete a Publisher ,all his books should be removed.
Similarly, deleting an Author should delete all his Books.

Currently ,I have the Author,Publisher and Book classes defined as below,
The Book has fields Author and Publisher .But ,Author or Publisher ,has no reference to their Books.
Do I have to redefine these classes?
How should I map the relation between Book and Publisher?

Any help appreciated

public class Book {
    private Long bookId;    
    private Author author;
    private Publisher publisher;
...
}

public class Publisher {
    private Long publisherId;   
    private String name;        
}

public class Author {
    private Long authorId;
    private String name;    
}

<class name="Book" table="BOOKS">
    <id name="bookId" column="BOOKID" type="long">
    <generator class="native"/>
    </id>
    ...
    <many-to-one name="publisher" class="Publisher"   column="PUBLISHERID" lazy="false" cascade="save-update"/>
  ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

审判长 2024-12-02 16:44:25

您需要映射一个集合 () 并将其设置为 cascade=all。这意味着如果删除集合的所有者,集合元素也会被删除。

请注意,您还可以选择使用注释而不是 xml。它将是@OneToMany(cascade=CascadeType.ALL)

You need to map a collection (<x-to-many>) and set it to cascade=all. This means that if you delete the owner of the collection, the collection elements are also deleted.

Note that you have also an option of using annotations rather than xml. Ther it will be @OneToMany(cascade=CascadeType.ALL)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文