在对象关系数据库 (Oracle) 中映射多对多关系

Map many to many relationship in object relational database (Oracle)

如果书籍和类别之间存在多对多关系,您可以将其映射到面向对象模型,如下所示基于 如何在对象数据库中设计多对多关系?

Book {
 Collection<Category> categories
}

Category {
 Collection<Books> books
}

要在 ORDBMS 的 table 中定义集合,您必须使用嵌套的 table。 (示例取自 oracle 网站 Sample Application Using Object-Relational Features

CREATE TABLE PurchaseOrder_objtab OF PurchaseOrder_objtyp (  /* Line 1 */
   PRIMARY KEY (PONo),                                       /* Line 2 */
   FOREIGN KEY (Cust_ref) REFERENCES Customer_objtab)        /* Line 3 */
   OBJECT IDENTIFIER IS PRIMARY KEY                          /* Line 4 */
   NESTED TABLE LineItemList_ntab STORE AS PoLine_ntab (     /* Line 5 */
     (PRIMARY KEY(NESTED_TABLE_ID, LineItemNo))              /* Line 6 */
     ORGANIZATION INDEX COMPRESS)                            /* Line 7 */
   RETURN AS LOCATOR                                         /* Line 8 */

但是最好保留一组引用而不是保留对象本身。

根据这个答案 嵌套 table Oracle 中的主键和外键 无法将外键添加到嵌套 table。

1) 那么在对象关系数据库 (Oracle) 中映射多对多关系的最佳方法是什么?

2) 如果答案是如上图在两个对象中保留两个集合,如何将其存储为引用集合而不直接将其存储为对象?

这就是我解决问题的方法。正如问题中提到的,当涉及到多对多映射时,每个对象的集合必须保存在两个对象中。

      /*-------Creating types-----------*/

     /*Incomplete type of BookCategory nested table- To keep a collection of categories which the book belongs*/
     CREATE TYPE BookCategory_tbltyp;
     /*Incomplete type of CategoryBook nested table - To keep a collection of books which the category contains*/
     CREATE TYPE CategoryBook_tbltyp;

     /*Creating Book type*/
     CREATE TYPE Book_objtyp AS OBJECT(
        Id VARCHAR2(6),
        Name VARCHAR2(30),
        BookCategoryList BookCategory_tbltyp
     );
     /*Creating Category type*/
     CREATE TYPE Category_objtyp AS OBJECT(
        Id VARCHAR2(6),
        Type VARCHAR2(30),
        CategoryBookList CategoryBook_tbltyp
     );

    /*Creating BookCategory object type*/
    CREATE TYPE BookCategory_objtyp AS OBJECT(
      CategoryId VARCHAR2(6),
      Category_ref REF Category_obj
    );

    /*Creating CategoryBook object type*/
    CREATE TYPE CategoryBook_objtyp AS OBJECT(
      BookId VARCHAR2(6),
      Book_ref REF Book_obj
    );

    /*Completing incomplete BookCategory and CategoryBook nested table types*/
    CREATE OR REPLACE TYPE BookCategory_tbltyp AS TABLE OF BookCategory_objtyp;

    CREATE OR REPLACE TYPE CategoryBook_tbltyp AS TABLE OF CategoryBook_objtyp;

    /*-------Creating tables-----------*/

     /*Creating book table with the nested collection table*/
     CREATE TABLE Book_objtab OF Book_objtyp(
        PRIMARY KEY (Id)
     )
     OBJECT IDENTIFIER IS PRIMARY KEY
     NESTED TABLE BookCategoryList STORE AS BookCategoryList_tab(
          PRIMARY KEY(NESTED_TABLE_ID, CategoryId)
        )
        ORGANIZATION INDEX COMPRESS)
     RETURN AS VALUE;

    /*Creating category table with the nested collection table*/
     CREATE TABLE Category_objtab OF Category_objtyp(
        PRIMARY KEY (Id)
     )
     OBJECT IDENTIFIER IS PRIMARY KEY
     NESTED TABLE CategoryBookList STORE AS CategoryBookList_tab(
          PRIMARY KEY(NESTED_TABLE_ID, BookId)
        )
        ORGANIZATION INDEX COMPRESS)
     RETURN AS VALUE;

    /*Foreign keys are not supported in nested tables. So the solution is to use SCOPE. 
According to oracle documentation SCOPE cannot be used in CREATE TABLE statement. 
So we have to use ALTER TABLE*/
    ALTER TABLE BookCategoryList_tab
        ADD (SCOPE FOR (Category_ref) IS Category_objtab);

    ALTER TABLE CategoryBookList_tab
        ADD (SCOPE FOR (Book_ref) IS Book_objtab);

SCOPE 约束与引用约束的不同之处在于,SCOPE 约束对引用对象没有依赖性。 例如:Category_objtab 中的行对象可能会被删除,即使它在嵌套的 table.

的 Category_ref 列中被引用也是如此

参考Sample Application Using Object-Relational Features了解更多信息。