<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Hiberate 多對多映射注解示例 > 原文: [https://howtodoinjava.com/hibernate/hibernate-many-to-many-mapping-using-annotations/](https://howtodoinjava.com/hibernate/hibernate-many-to-many-mapping-using-annotations/) **在兩個實體之間建立多對多 Hiberate 的映射**,其中一個實體可以與多個其他實體實例相關。 例如,對于訂閱服務,`SubscriptionEntity`和`ReaderEntity`可以是兩種類型的實體。 任何訂閱都可以有多個閱讀器,其中一個閱讀器可以訂閱多個訂閱。 在此 hibernate 教程中,我們將學習使用 hibernate 在數據庫中創建多對多映射。 ```java Table of contents Hibernate many to many mapping design Owner entity Mapped entity Configure entities in hibernate config file Demo ``` ## 1\. Hiberate 多對多映射設計 為了演示使用 Hiberate 注解的多對多映射,我們將關聯兩個實體,即`ReaderEntity`和`SubscriptionEntity`。 他們的數據庫架構應如下所示。 使用這些表,任何應用都可以保存讀者和訂閱之間的多個關聯。 ![many-to-many-hibernate-mapping](https://img.kancloud.cn/5f/15/5f15a2759e2714e1fd3f35353d4ed75c_671x160.png "many-to-many-hibernate-mapping") ## 2\. 所有者實體 所有者實體是**負責建立關聯并維護它的實體**。 在我們的情況下,我將`ReaderEntity`設置為所有者實體。 `@JoinTable`注解已用于建立此關聯。 ```java package hibernate.test.manyToMany.joinTable; import java.io.Serializable; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.UniqueConstraint; @Entity(name = "ReaderEntity") @Table(name = "READER", uniqueConstraints = { @UniqueConstraint(columnNames = "ID"), @UniqueConstraint(columnNames = "EMAIL") }) public class ReaderEntity implements Serializable { private static final long serialVersionUID = -1798070786993154676L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "ID", unique = true, nullable = false) private Integer readerId; @Column(name = "EMAIL", unique = true, nullable = false, length = 100) private String email; @Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100) private String firstName; @Column(name = "LAST_NAME", unique = false, nullable = false, length = 100) private String lastName; @ManyToMany(cascade=CascadeType.ALL) @JoinTable(name="READER_SUBSCRIPTIONS", joinColumns={@JoinColumn(referencedColumnName="ID")} , inverseJoinColumns={@JoinColumn(referencedColumnName="ID")}) private Set<SubscriptionEntity> subscriptions; //Getters and setters } ``` ## 3\. 映射實體 我們的映射實體是`SubscriptionEntity`,它使用“`mappingBy`”屬性映射到`ReaderEntity`。 ```java package hibernate.test.manyToMany.joinTable; import java.io.Serializable; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.UniqueConstraint; @Entity(name = "SubscriptionEntity") @Table(name = "SUBSCRIPTION", uniqueConstraints = { @UniqueConstraint(columnNames = "ID")}) public class SubscriptionEntity implements Serializable { private static final long serialVersionUID = -6790693372846798580L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "ID", unique = true, nullable = false) private Integer subscriptionId; @Column(name = "SUBS_NAME", unique = true, nullable = false, length = 100) private String subscriptionName; @ManyToMany(mappedBy="subscriptions") private Set<ReaderEntity> readers; //Getters and setters } ``` ## 4\. 在 Hiberate 配置文件中配置實體 我們使兩個實體都可以在運行時使用。 為此,我們必須將它們添加到`hibernate.cfg.xml`文件中。 ```java <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest</property> <property name="hibernate.connection.password">XXXXXX</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping class="hibernate.test.manyToMany.joinTable.ReaderEntity"/> <mapping class="hibernate.test.manyToMany.joinTable.SubscriptionEntity"/> </session-factory> </hibernate-configuration> ``` ## 5\. Hiberate 多對多注解映射示例 現在,該測試代碼了。 我編寫了以下代碼來測試上述實體及其多對多關系。 ```java package hibernate.test.manyToMany; import hibernate.test.HibernateUtil; import hibernate.test.manyToMany.joinTable.*; import java.util.HashSet; import java.util.Set; import org.hibernate.Session; public class TestJoinTable { public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); //Add subscription SubscriptionEntity subOne = new SubscriptionEntity(); subOne.setSubscriptionName("Entertainment"); SubscriptionEntity subTwo = new SubscriptionEntity(); subTwo.setSubscriptionName("Horror"); Set<SubscriptionEntity> subs = new HashSet<SubscriptionEntity>(); subs.add(subOne); subs.add(subTwo); //Add readers ReaderEntity readerOne = new ReaderEntity(); readerOne.setEmail("demo-user1@mail.com"); readerOne.setFirstName("demo"); readerOne.setLastName("user"); ReaderEntity readerTwo = new ReaderEntity(); readerTwo.setEmail("demo-user2@mail.com"); readerTwo.setFirstName("demo"); readerTwo.setLastName("user"); Set<ReaderEntity> readers = new HashSet<ReaderEntity>(); readers.add(readerOne); readers.add(readerTwo); readerOne.setSubscriptions(subs); readerTwo.setSubscriptions(subs); session.save(readerOne); session.save(readerTwo); session.getTransaction().commit(); HibernateUtil.shutdown(); } } ``` 程序輸出: ```java Hibernate: insert into READER (EMAIL, FIRST_NAME, LAST_NAME) values (?, ?, ?) Hibernate: insert into SUBSCRIPTION (SUBS_NAME) values (?) Hibernate: insert into SUBSCRIPTION (SUBS_NAME) values (?) Hibernate: insert into READER (EMAIL, FIRST_NAME, LAST_NAME) values (?, ?, ?) Hibernate: insert into READER_SUBSCRIPTIONS (readers_ID, subscriptions_ID) values (?, ?) Hibernate: insert into READER_SUBSCRIPTIONS (readers_ID, subscriptions_ID) values (?, ?) Hibernate: insert into READER_SUBSCRIPTIONS (readers_ID, subscriptions_ID) values (?, ?) Hibernate: insert into READER_SUBSCRIPTIONS (readers_ID, subscriptions_ID) values (?, ?) ``` [下載源碼](https://drive.google.com/file/d/0B7yo2HclmjI4ZHdoLVF2Zk1Xc1E/view?usp=drive_web "download example of many to many mapping") 在此示例中,我們了解了在 Hiberate 中**使用多對多注解來連接表**。 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看