的無需擔心持續性,我已經設計了一個類來保存的(不同尺寸)的容器映射不同產品的數量,該產品可以保持在如何定義Hibernate映射在地圖一個地圖
class Inventory {
Long id;
Map <Long, Map<Long,Integer>> productQuantityByContainer; // ProductId->(ContainerId -> quantity)
}
與產品和容器類作爲
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能夠從我的Inventory類中持久保存信息,是否可以使我的設計和映射如下所示? (我創建兩個類ContainerQuantity和數量具有相同名稱的兩個映射表如下圖所示),請批評/下面看看我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
是否有可能與安來實現這一otations? –