Hibernate多對多映射關係(5)

來源:互聯網
上載者:User

標籤:mit   apply   items   關聯   實體類   logs   class   關係   code   

單向 n-n

  n-n 的關聯必須使用串連表

  與 1-n 映射類似,必須為 set 集合元素添加 key 子項目,指定 CATEGORIES_ITEMS 表中參照 CATEGORIES 表的外鍵為 CATEGORIY_ID. 與 1-n 關聯映射不同的是,建立 n-n 關聯時, 集合中的元素使用 many-to-many. many-to-many 子項目的 class 屬性指定 items 集合中存放的是 Item 對象, column 屬性指定 CATEGORIES_ITEMS 表中參照 ITEMS 表的外鍵為 ITEM_ID

雙向 n-n

  雙向 n-n 關聯需要兩端都使用集合屬性

  雙向n-n關聯必須使用串連表

  集合屬性應增加 key 子項目用以映射外鍵列, 集合元素裡還應增加many-to-many子項目關聯實體類

   在雙向 n-n 關聯的兩邊都需指定串連表的表名及外鍵列的列名. 兩個集合元素 set 的 table 元素的值必須指定,而且必須相同。set元素的兩個子項目:key 和 many-to-many 都必須指定 column 屬性,其中,key 和 many-to-many 分別指定本持久化類和關聯類別在串連表中的外鍵列名,因此兩邊的 key 與 many-to-many 的column屬性交叉相同。也就是說,一邊的set元素的key的 cloumn值為a,many-to-many 的 column 為b;則另一邊的 set 元素的 key 的 column 值 b,many-to-many的 column 值為 a.

  對於雙向 n-n 關聯, 必須把其中一端的 inverse 設定為 true, 否則兩端都維護關聯關係可能會造成主鍵衝突.

部分代碼:

Category.java

private Integer id;private String name;private Set<Item> items = new HashSet<>();

Item.java

private Integer id;private String name;private Set<Category> categories = new HashSet<>();

Category.hbm.xml

<!-- table: 指定中間表 --><set name="items" table="CATEGORIES_ITEMS">        <key>             <column name="C_ID" />         </key>  <!-- 使用 many-to-many 指定多對多的關聯關係. column 執行 Set 集合中的持久化類在中間表的外鍵列的名稱  -->       <many-to-many class="Item" column="I_ID"></many-to-many></set>

Item.hbm.xml

<set name="categories" table="CATEGORIES_ITEMS" inverse="true">       <key column="I_ID"></key>        <many-to-many class="Category" column="C_ID"></many-to-many></set>

HibernateTest.java

public class HibernateTest {    private SessionFactory sessionFactory;    private Session session;    private Transaction transaction;        @Before    public void init(){        Configuration configuration = new Configuration().configure();        ServiceRegistry serviceRegistry =                 new ServiceRegistryBuilder().applySettings(configuration.getProperties())                                            .buildServiceRegistry();        sessionFactory = configuration.buildSessionFactory(serviceRegistry);                session = sessionFactory.openSession();        transaction = session.beginTransaction();    }        @After    public void destroy(){        transaction.commit();        session.close();        sessionFactory.close();    }        @Test    public void testGet(){        Category category = (Category) session.get(Category.class, 1);        System.out.println(category.getName());                 //需要串連中間表        Set<Item> items = category.getItems();        System.out.println(items.size());     }        @Test    public void testSave(){        Category category1 = new Category();        category1.setName("C-AA");        Category category2 = new Category();        category2.setName("C-BB");                Item item1 = new Item();        item1.setName("I-AA");                Item item2 = new Item();        item2.setName("I-BB");                //設定關聯關係        category1.getItems().add(item1);        category1.getItems().add(item2);                category2.getItems().add(item1);        category2.getItems().add(item2);                item1.getCategories().add(category1);        item1.getCategories().add(category2);                item2.getCategories().add(category1);        item2.getCategories().add(category2);                //執行儲存操作        session.save(category1);        session.save(category2);                session.save(item1);        session.save(item2);    }}

 

Hibernate多對多映射關係(5)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.