【问题标题】:How to define hibernate mapping for a map of maps如何为地图地图定义休眠映射
【发布时间】:2016-11-29 00:51:28
【问题描述】:

不用担心持久性,我设计了一个类来保存由(不同大小的)容器映射的不同产品的数量,产品可以保存在这些容器中

class Inventory {
  Long                             id;
  Map <Long, Map<Long,Integer>>    productQuantityByContainer; // ProductId->(ContainerId -> quantity)
}

Product 和 Container 类为

class Product {
  Long  id;
  String name;
}

class Container {
  Long     id;
  Double   size;
}

后两者具有与其相应数据库表的直接 Hibernate 映射。我需要使用 Hibernate 映射文件(hbm.xml)并且我的数据库有以下信息(不考虑 Hibernate 的约束):

Product table
--------------
ID|Name
1  Milk
2  Orange Juice

Container table
---------------
ID |Size(oz)
10  32  
11  64

Inventory table (first 2 columns form the PK)
---------------
Product-ID(FK) | Container-ID(FK) | Quantity
  1              10                   25
  1              11                   15
  2              10                   33

我的问题是:我了解 Hibernate 不允许集合映射的持久性。因此,要使 Hibernate 持久保存我的 Inventory 类中的信息,我的设计和映射如下所示就足够了吗? (我使用两个同名映射表创建了两个类 ContainerQuantity 和 Quantity,如下所示)请在下面评论/检查我的 hbm 映射。

class Inventory {   // <--- Redesigned --->
  Long       id;
  Map <Long, ContainerQuantity>    cqsByProduct; // ProductId->ContainerQty
}

<class name="Inventory"> <!-- hbm mapping -->
   <id name="id"/>
   <map name="cqsByProduct">
      <key column="Inventory_ID" not-null="true"/>
      <map-key column="Product_ID"
      <one-to-many class="ContainerQuantity"/>
   </map>
</class>

Inventory table (in Database)
---------------
  id
 90210
 90211

class ContainerQuantity {
  Long                  id;
  Map<Long,Integer>     quantitiesByContainer;
}

<class name="ContainerQuantity"> <!-- hbm mapping -->
   <id name="id"/>
   <map name="quantitiesByContainer">
      <key column="CQ_ID" not-null="true"/>
      <map-key column="Container_ID"
      <one-to-many class="Quantity"/>
   </map>
</class>

ContainerQuantity table (for holding the cqsByProduct collection)
------------------------
   id     | Product_ID  | Inventory_ID (FK to collection owner)  
  101          1            90210
  102          1            90210
  103          2            90210

class Quantity {
  Integer               quantity;
  Container             container; 
  ContainerQuantity     cq;        
}

<class name="Quantity"> <!-- hbm mapping -->
    <composite-id>
       <key-many-to-one name="container" class="Container" column="Container_ID" />
       <key-many-to-one name="cq" class="ContainerQuantity" column="CQ_ID" />
    </composite-id>     
</class>

Quantity table  (for holding quantitiesByContainer collection)
--------------------
 Container_ID(FK) | Quantity | CQ_ID (FK to collection owner)
    10               25         101
    11               15         102
    10               33         103

【问题讨论】:

    标签: hibernate


    【解决方案1】:

    经过很多磨难(并且没有关于 SO 的答案),我找到了解决我自己问题的方法。希望这将有助于将来的其他人。我确实简化了我的设计以摆脱 Quantity 类,因为 ContainerQuantity 中的映射可以只是一个值集合。然而,值得注意的是,没有 Quantity 类并不能阻止我们在数据库中拥有“数量”表。

    // classes defined in package hibtest
    public class Inventory {
      Long            id;
      String          name;
      Map <Product, ContainerQuantity>    cqsByProduct;
    
      public Inventory()
      //..getters and setters for each of above
    }
    
    public class ContainerQuantity {
       Long                  id;
       Inventory             inventory;
       Map<Container,Integer>     quantitiesByContainer;
    
       public ContainerQuantity()
      //..getters and setters for each of above
    }
    

    最困难的部分是正确的 Hibernate 映射,结果是:

    <hibernate-mapping>
      <class name="hibtest.Inventory" table="INVENTORY">
         <id name="id" column="INVENTORY_ID">
            <generator class="sequence">
                <param name="sequence">DON.NEXT_INVENTORY_ID</param>
            </generator>
         </id>
         <property name="name" column="name" type="string"/>
         <map name="cqsByProduct" inverse="true" cascade="all"> <!-- inverse=true ensures INVENTORY_ID in CONTAINERQUANTITY table is set by hibtest.ContainerQuantity -->
            <key column="INVENTORY_ID" not-null="true"/>
            <map-key-many-to-many column="PRODUCT_ID" class="hibtest.Product"/>
            <one-to-many class="hibtest.ContainerQuantity"/>
         </map>
      </class>
    
      <class name="hibtest.ContainerQuantity" table="CONTAINERQUANTITY">
         <id name="id" column="CQ_ID">
            <generator class="sequence">
                <param name="sequence">DON.NEXT_CONTAINERQUANTITY_ID</param>
            </generator>
         </id>
    
         <many-to-one name="inventory" class="hibtest.Inventory" column="INVENTORY_ID" not-null="true" /> <!-- column will be set when row inserted into CONTAINERQUANTITY table -->
    
         <map name="quantitiesByContainer" table="QUANTITY" cascade="all" > <!--  inverse is false so CQ_ID in QUANTITY will be set by ContainerQuantity -->
           <key column="CQ_ID" not-null="true"/>
           <map-key-many-to-many column="CONTAINER_ID" class="hibtest.Container"/>       
           <element column="quantity" type="int"/>
         </map>
      </class>
     </hibernate-mapping>
    

    我的表看起来是这样(Oracle sql):

    create table product
    (
      id               NUMBER(22) not null,
      name             VARCHAR2(32) not null);
    
    create table container
    (
      id             NUMBER(22) not null,
      name           VARCHAR2(32) not null);
    
    create table inventory
    (
     INVENTORY_ID             NUMBER(22) not null,
     name                     VARCHAR2(32) not null);
    
    create table containerquantity
    (
     cq_id                    NUMBER(22) not null,
     product_id               NUMBER(22),
     inventory_id             NUMBER(22));
    
    create table quantity
    (
     quantity                 NUMBER(10) not null,
     container_id             NUMBER(22) not null,
     cq_id                    NUMBER(22) not null);
    

    这里是驱动代码:

    private static void persist(Session session){
        Product product1 = new Product();
        Product product2 = new Product();       
        product1.setName("Apple");
        product2.setName("Milk");
    
        Container c1 = new Container();
        Container c2 = new Container();
        c1.setName("32 oz");
        c2.setName("64 oz");
    
        //-- Make container-qty1 and add 2 quantity entries into it
        ContainerQuantity cq1 = new ContainerQuantity();
    
        Map<Container,Integer>     quantitiesByContainer1 = new HashMap<Container,Integer>();
    
        quantitiesByContainer1.put(c1, Integer.valueOf(25));
        quantitiesByContainer1.put(c2, Integer.valueOf(15));
        cq1.setQuantitiesByContainer(quantitiesByContainer1);
    
        //-- Make container-qty2 and add 1 quantity entry into it
        ContainerQuantity cq2 = new ContainerQuantity();
    
        Map<Container,Integer>     quantitiesByContainer2 = new HashMap<Container,Integer>();
    
        quantitiesByContainer2.put(c1, Integer.valueOf(33));
        cq2.setQuantitiesByContainer(quantitiesByContainer2);
    
        //-- Make Inventory object 
        Inventory inv = new Inventory(); inv.setName("foods");
        Map <Product, ContainerQuantity>    cqsByProduct = new HashMap<Product, ContainerQuantity>();
        cqsByProduct.put(product1, cq1);
        cqsByProduct.put(product2, cq2);
    
        //-- Set back-reference to inventory on ContainerQuantity objects 
        cq1.setInventory(inv);
        cq2.setInventory(inv);
    
        inv.setCqsByProduct(cqsByProduct);      
    
        session.save(product1);
        session.save(product2);
        session.save(c1);
        session.save(c2);
    
        session.save(inv);      
    }
    

    【讨论】:

    • 是否可以通过注解来实现?
    猜你喜欢
    • 2015-12-21
    • 2011-01-20
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多