1、创建实体类(Customer.java、Orders.java)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package wck.stu.vo.oneToMany_single; import java.util.HashSet; import java.util.Set; public class Customer { private String id = ""; private String cName = ""; private String bank = ""; private String phone = ""; private Set orders = new HashSet(); public String getId() { return id; } public void setId(String id) { this.id = id; } public String getcName() { return cName; } public void setcName(String cName) { this.cName = cName; } public String getBank() { return bank; } public void setBank(String bank) { this.bank = bank; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Set getOrders() { return orders; } public void setOrders(Set orders) { this.orders = orders; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package wck.stu.vo.oneToMany_single; 2 3 public class Orders { 4 private String id = ""; 5 6 private String orderNo = ""; 7 8 private Double money = 0.0; 9 10 private Customer customer; 11 12 public String getId() { 13 return id; 14 } 15 16 public void setId(String id) { 17 this.id = id; 18 } 19 20 public String getOrderNo() { 21 return orderNo; 22 } 23 24 public void setOrderNo(String orderNo) { 25 this.orderNo = orderNo; 26 } 27 28 public Double getMoney() { 29 return money; 30 } 31 32 public void setMoney(Double money) { 33 this.money = money; 34 } 35 36 public Customer getCustomer() { 37 return customer; 38 } 39 40 public void setCustomer(Customer customer) { 41 this.customer = customer; 42 } 43 }
2、配置文件(Customer.hbm.xml、Orders.hbm.xml)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping> 7 <class name="wck.stu.vo.oneToMany_single.Customer" table="CUSTOMER"> 8 <id name="id" type="java.lang.String"> 9 <column name="C_ID" length="32" not-null="true"></column> 10 <generator class="uuid.hex"></generator> 11 </id> 12 <property column="CNAME" name="cName" type="string" length="100"/> 13 <property column="BANK" name="bank" type="string" length="100"/> 14 <property column="PHONE" name="phone" type="string" length="20"/> 15 <!--一对多双向关联映射customer到orders,单的一方配置 --> 16 <set cascade="all" inverse="true" lazy="false" name="orders"> 17 <key column="CUSTOMER_ID" /> 18 <one-to-many class="wck.stu.vo.oneToMany_single.Orders" /> 19 </set> 20 </class> 21 </hibernate-mapping>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping> 7 <class name="wck.stu.vo.oneToMany_single.Orders" table="ORDERS"> 8 <id name="id" type="java.lang.String"> 9 <column name="C_ID" length="32" not-null="true"></column> 10 <generator class="uuid.hex"></generator> 11 </id> 12 <property column="ORDERNO" name="orderNo" type="java.lang.String" length="17"/> 13 <property column="MONEY" name="money" type="java.lang.Double" /> 14 <many-to-one name="customer" class="wck.stu.vo.oneToMany_single.Customer" column="CUSTOMER_ID" cascade="all" lazy="false" not-null="true"/> 15 </class> 16 </hibernate-mapping>
3、测试代码
1 public String saveCustomerInfo() { 2 Customer customer = new Customer(); 3 customer.setcName("清华大学出版社"); 4 customer.setBank("9559501012356789"); 5 customer.setPhone("010-62772015"); 6 Orders order = new Orders(); 7 order.setOrderNo(new Long(System.currentTimeMillis()).toString()); 8 order.setMoney(new Double(rnd.nextDouble()*10000)); 9 order.setCustomer(customer); 10 customer.getOrders().add(order); 11 oneToManyDao.saveCustormerInfo(customer); 12 return customer.getId(); 13 }