Hibernate多对多关联(亲测成功)

1、创建实体(Items.java、Orders1.java)

 1 package wck.stu.vo.manyToMany;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Items {
 7     private String id = "";
 8     
 9     private String itemNo = "";
10     
11     private String itemName = "";
12     
13     private Set<Object> orders = new HashSet<Object>();
14 
15     public String getId() {
16         return id;
17     }
18 
19     public void setId(String id) {
20         this.id = id;
21     }
22 
23     public String getItemNo() {
24         return itemNo;
25     }
26 
27     public void setItemNo(String itemNo) {
28         this.itemNo = itemNo;
29     }
30 
31     public String getItemName() {
32         return itemName;
33     }
34 
35     public void setItemName(String itemName) {
36         this.itemName = itemName;
37     }
38 
39     public Set<Object> getOrders() {
40         return orders;
41     }
42 
43     public void setOrders(Set<Object> orders) {
44         this.orders = orders;
45     }
46     
47 }
Items.java
 1 package wck.stu.vo.manyToMany;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Orders1 {
 7     private String id = "";
 8     
 9     private String orderNo = "";
10     
11     private Double money = 0.0;
12     
13     private Set<Object> items = new HashSet<Object>();
14 
15     public String getId() {
16         return id;
17     }
18 
19     public void setId(String id) {
20         this.id = id;
21     }
22 
23     public String getOrderNo() {
24         return orderNo;
25     }
26 
27     public void setOrderNo(String orderNo) {
28         this.orderNo = orderNo;
29     }
30 
31     public Double getMoney() {
32         return money;
33     }
34 
35     public void setMoney(Double money) {
36         this.money = money;
37     }
38 
39     public Set<Object> getItems() {
40         return items;
41     }
42 
43     public void setItems(Set<Object> items) {
44         this.items = items;
45     }
46 }
Orders.java

 

2、配置文件(Items.hbm.xml、Orders.hbm.xml)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 3 <hibernate-mapping>
 4   <class name="wck.stu.vo.manyToMany.Items" table="ITEMS">
 5         <id name="id" type="java.lang.String">
 6             <column name="C_ID" length="32" not-null="true"></column>
 7             <generator class="uuid.hex"></generator>
 8         </id>
 9     <property column="ITEMNO" name="itemNo" type="string"/>
10     <property column="ITEMNAME" name="itemName" type="string"/>
11     <!--映射Items到Orders的多对多单向关联-->
12     <set cascade="save-update" inverse="true" lazy="true" name="orders" table="selecteditems">
13       <key column="ITEMID"/>
14       <many-to-many class="wck.stu.vo.manyToMany.Orders1" column="ORDERID"/>
15     </set>
16   </class>
17 </hibernate-mapping>
Items.hbm.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 3 <hibernate-mapping>
 4   <class name="wck.stu.vo.manyToMany.Orders1" table="ORDERS1">
 5         <id name="id" type="java.lang.String">
 6             <column name="C_ID" length="32" not-null="true"></column>
 7             <generator class="uuid.hex"></generator>
 8         </id>
 9     <property column="ORDERNO" name="orderNo" type="string" length="100"/>
10     <property column="MONEY" name="money" type="double" length="100"/>
11     <!--映射Orders到Items的多对多单向关联-->
12     <set cascade="save-update" lazy="true" name="items" table="selecteditems">
13       <key column="ORDERID"/>
14       <many-to-many class="wck.stu.vo.manyToMany.Items" column="ITEMID"/>
15     </set>
16   </class>
17 </hibernate-mapping>
Orders1.hbm.xml


3、测试代码

 1 public void saveInfo() {
 2         String itemNo = "001";
 3         String itenName = "002";
 4         Set<Object> set = new HashSet<Object>();
 5         for (int i = 0; i < 2; i++) {
 6             Items item = new Items();
 7             item.setItemName(itenName+i);
 8             item.setItemNo(itemNo);
 9             set.add(item);
10         }
11         String orderNo = new Long(System.currentTimeMillis()).toString();
12         Random rnd = new Random();
13         double money = new Double(rnd.nextDouble()*10000);
14         ois.saveOrdersInfo(orderNo, money, set);
15     }
16 
17 public String saveOrdersInfo(String orderNo, Double money, Set<Object> items) {
18         Orders1 order = new Orders1();
19         order.setOrderNo(orderNo);
20         order.setMoney(money);
21         order.setItems(items);
22         return manyToManyDao.saveOrdersInfo(order);
23     }

 

你可能感兴趣的:(Hibernate多对多关联(亲测成功))