<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Hibernate 4 的實體關系 > 原文: [https://javabeginnerstutorial.com/hibernate/entity-relations-with-hibernate-4/](https://javabeginnerstutorial.com/hibernate/entity-relations-with-hibernate-4/) 上次我介紹了注解而不是 XML 配置。 現在,我將更深入地研究并展示如何創建實體關系并將其映射到數據庫。 如果在示例中查看**圖書**實體,您可能會想到:“為什么將圖書的作者存儲為字符串?” 你是對的。 這使得作者查詢書籍幾乎是不可能的,或者至少沒有表現得那么出色。 而且兩次輸入之間的錯別字會使情況更糟。 在本文中,我將進一步舉例說明,然后將作者的字符串提取到另一個實體中。 ## 作者實體 讓我們從一個擁有作者信息的新實體開始。 為簡單起見,它將僅包含作者的姓名和作者創建的書籍列表。 ```java @Entity @Table(name = "AUTHORS") public class Author { ?? @Id ?? private String name; ?? @ManyToMany(mappedBy = "authors") ?? private final List<Book> books = new ArrayList<>(); ?? private Author() { ?? } ?? public Author(String name) { ?????? this.name = name; ?? } ?? public String getName() { ?????? return this.name; ?? } ?? public void setName(String name) { ?????? this.name = name; ?? } ?? public List<Book> getBooks() { ?????? return this.books; ?? } ?? @Override ?? public String toString() { ?????? return MessageFormat.format("{0} has written {1} book(s).", this.name, this.books.size()); ?? } } ``` 如您所見,這里有一個新的注解:`@ManyToMany`。 這告訴 Hibernate `Author`實體映射到其他`Book`實體。 `mappingBy`屬性用于告訴 Hibernate 哪個實體是關系的所有者。 這主要用于*一對多*或*多對一*關系中。 ## 將作者添加到`Book`實體 現在,將作者字符串更改為作者列表。 它看起來與`Author`實體完全相同: ```java @Entity @Table(name = "BOOKS") public class Book { ?? @Id ?? private String isbn; ?? private String title; ?? @ManyToMany ?? private final List<Author> authors = new ArrayList<>(); ?? @Temporal(TemporalType.DATE) ?? @Column(name = "PUBLISHED_DATE") ?? private Date published; ?? private Book() { ?? } ?? public Book(String isbn, String title, Date published) { ?????? this.isbn = isbn; ?????? this.title = title; ?????? this.published = published; ?? } ?? public String getIsbn() { ?????? return this.isbn; ?? } ?? public String getTitle() { ?????? return this.title; ?? } ?? public void setTitle(String title) { ?????? this.title = title; ?? } ?? public List<Author> getAuthors() { ?????? return this.authors; ?? } ?? public void setIsbn(String isbn) { ?????? this.isbn = isbn; ?? } ?? public Date getPublished() { ?????? return this.published; ?? } ?? public void setPublished(Date published) { ?????? this.published = published; ?? } ?? @Override ?? public String toString() { ?????? final String authorNames = this.authors.stream().map(Author::getName).collect(Collectors.joining(", ")); ?????? return MessageFormat.format("{0} by {1} (ISBN: {2}), published {3}", this.title, authorNames, this.isbn, this.published); ?? } } ``` 在這里,我沒有在`@ManyToMany`注解中添加任何參數,因為作者是所有者。 ## 關系類型 當然,**多對多**關系不是唯一可用的關系。 如果需要,您也可以選擇**一對一**,**一對多**或**多對一**。 ### 一對一關系 對于一個實體,此關系類型僅包含一種引用類型,而對于另一實體也是如此。 在這種情況下,您可以選擇用于存儲引用的實體。例如,如果我將 ISBN 提取為具有其他一些屬性的單獨實體,則可能是書籍與 ISBN 號之間的關系:一本書具有一本 ISBN 和一本 ISBN 準確地指一本書。 ### 一對多和多對一關系 在這種情況下,一個實體在其他實體中具有許多引用。 如果一本書只能有一位作者,那就是這種情況。 在這種情況下,引用 I??D 將存儲在`BOOKS`表中,因為有一本書參考了其作者。 我們在作者實體中使用*一對多*,在`Book`實體中使用*多對一*。 ### 多對多關系 如您在第一個示例中所看到的,這種關系有點復雜。 在這里,我們需要一個單獨的表格來包含書籍和作者之間的引用。 這就是所謂的**聯接表**。 該表由 Hibernate 自動維護,但是您可以告訴該表如何命名。 如果您不選擇名稱,則 Hibernate 會使用實體名稱,并用下劃線將其分開,所有者名稱為站點實體。 在此示例中,聯接表名為:`BOOKS_AUTHORS`。 ## 更改`main`方法 由于實體已更改,因此我也必須更改`Main`類的`main`方法。 ```java final Book book = new Book("9781617291999", "Java 8 in Action", new Date()); session.beginTransaction(); Arrays.stream("Raoul-Gabriel Urma,Mario Fusco,Alan Mycroft".split(",")).map(name -> new Author(name)).forEach(author -> { author.getBooks().add(book); book.getAuthors().add(author); session.save(author); }); session.save(book); session.getTransaction().commit(); ``` 如您所見,我使用一些 lambda 來動態創建作者。 有趣的部分是最后一個語句,`forEach`塊:我將這本書添加到當前作者的圖書列表中,然后將當前作者添加到這本書的作者列表中。 以后需要在數據庫中一起查找書籍和作者時,需要使用此引用(如果您手動查詢或使用 Hibernate 加載數據集)。 現在的結果與上次有所不同: ```java ---- Storing 1 books in the database Java 8 in Action by Raoul-Gabriel Urma, Mario Fusco, Alan Mycroft (ISBN: 9781617291999), published 2015.06.29\. 16:57 ---- ``` 如果我放棄聲明`book.getAuthors().add(author);`,則結果不包含作者的姓名: ```java ---- Storing 1 books in the database Java 8 in Action by (ISBN: 9781617291999), published 2015.06.29\. 16:58 ---- ``` ## 總結 如您所見,有很多選項可以將實體關系映射到規范化數據庫。 下次,我將向您展示如何將實體繼承映射到數據庫。 #### 代碼下載 您可以從 Github [此處](https://github.com/JBTAdmin/Hibernate)下載特定章節的代碼。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看