如何为地图映射定义hibernate映射
不用担心持久性,我设计了一个类来保存由(不同大小)容器映射的不同产品的数量,这些容器可以保存产品如何为地图映射定义hibernate映射,hibernate,Hibernate,不用担心持久性,我设计了一个类来保存由(不同大小)容器映射的不同产品的数量,这些容器可以保存产品 class Inventory { Long id; Map <Long, Map<Long,Integer>> productQuantityByContainer; // ProductId->(ContainerId -> quantity) } 后两个具有到相应数据库表的直接Hiber
class Inventory {
Long id;
Map <Long, Map<Long,Integer>> productQuantityByContainer; // ProductId->(ContainerId -> quantity)
}
后两个具有到相应数据库表的直接Hibernate映射。我需要使用Hibernate映射文件(hbm.xml),我的数据库包含以下信息(不考虑Hibernate的约束):
我的问题是:我理解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
类资源清册{//
长id;
映射cqsByProduct;//ProductId->ContainerQty
}
在经历了许多磨难之后(没有答案),我找到了自己问题的答案。希望这将有助于其他人在未来。我确实简化了我的设计以摆脱Quantity类,因为ContainerQuantity中的map可以只是一个值集合。然而,值得注意的是,没有Quantity类并不能阻止我们在数据库中拥有“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
}
以下是驾驶员代码:
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);
}
private static void persist(会话){
product1=新产品();
product2=新产品();
产品1.设置名称(“苹果”);
产品2.设置名称(“牛奶”);
容器c1=新容器();
容器c2=新容器();
c1.设定名称(“32盎司”);
c2.设定名称(“64盎司”);
//--制作容器-qty1,并在其中添加2个数量条目
ContainerQuantity cq1=新的ContainerQuantity();
Map quantitiesByContainer1=新HashMap();
quantitiesByContainer1.put(c1,Integer.valueOf(25));
quantitiesByContainer1.put(c2,Integer.valueOf(15));
cq1.设置QuantitiesByContainer(QuantiesByContainer1);
//--将container-qty2设置为空,并在其中添加1个数量条目
ContainerQuantity cq2=新的ContainerQuantity();
Map quantitiesByContainer2=新HashMap();
quantitiesByContainer2.put(c1,Integer.valueOf(33));
cq2.设置QuantitiesByContainer(QuantiesByContainer2);
//--使库存对象
库存库存=新库存();库存集合名称(“食品”);
Map cqsByProduct=newhashmap();
cqsByProduct.put(product1,cq1);
cqsByProduct.put(product2,cq2);
//--在ContainerQuantity对象上设置对库存的引用
cq1.设置库存(inv);
cq2.设置库存(inv);
inv.setCqsByProduct(cqsByProduct);
session.save(product1);
session.save(product2);
session.save(c1);
会话.保存(c2);
会话保存(inv);
}
在经历了许多磨难之后(没有任何答案),我找到了自己问题的答案。希望这将有助于其他人在未来。我确实简化了我的设计以摆脱Quantity类,因为ContainerQuantity中的map可以只是一个值集合。然而,值得注意的是,没有Quantity类并不能阻止我们在数据库中拥有“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
}
以下是驾驶员代码:
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);
}
private static void persist(会话){
product1=新产品();
product2=新产品();
产品1.设置名称(“苹果”);
产品2.设置名称(“牛奶”);
容器c1=新容器();
容器c2=新容器();
c1.设定名称(“32盎司”);
c2.设定名称(“64盎司”);
//--制作容器-qty1,并在其中添加2个数量条目
ContainerQuantity cq1=新的ContainerQuantity();
Map quantitiesByContainer1=新HashMap();
quantitiesByContainer1.put(c1,Integer.valueOf(25));
quantitiesByContainer1.put(c2,Integer.valueOf(15));
cq1.设置QuantitiesByContainer(QuantiesByContainer1);
//--将container-qty2设置为空,并在其中添加1个数量条目
ContainerQuantity cq2=新的ContainerQuantity();
Map quantitiesByContainer2=新HashMap();
quantitiesByContainer2.put(c1,Integer.valueOf(33));
cq2.设置QuantitiesByContainer(QuantiesByContainer2);
//--使库存对象
库存库存=新库存();库存集合名称(“食品”);
Map cqsByProduct=newhashmap();
cqsByProduct.put(product1,cq1);
cqsByProduct.put(product2,cq2);
//--在ContainerQuantity对象上设置对库存的引用
cq1.设置库存(inv);
cq2.设置库存(inv);
inv.setCqsByProduct(cqsByProduct);
session.save(product1);
session.save(product2);
session.save(c1);
会话.保存(c2);
会话保存(inv);
}
是否可以通过注释实现?是否可以通过注释实现?
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);
}