本文共 2144 字,大约阅读时间需要 7 分钟。
1、创建实体类(Customer.java、Orders.java)
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;
}
}
Customer.java
package wck.stu.vo.oneToMany_single;
public class Orders {
private String id = "";
private String orderNo = "";
private Double money = 0.0;
private Customer customer;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
Orders.java
2、配置文件(Customer.hbm.xml、Orders.hbm.xml)
/p>
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Customer.hbm.xml
/p>
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Orders.hbm.xml
3、测试代码
public String saveCustomerInfo() {
Customer customer = new Customer();
customer.setcName("清华大学出版社");
customer.setBank("9559501012356789");
customer.setPhone("010-62772015");
Orders order = new Orders();
order.setOrderNo(new Long(System.currentTimeMillis()).toString());
order.setMoney(new Double(rnd.nextDouble()*10000));
order.setCustomer(customer);
customer.getOrders().add(order);
oneToManyDao.saveCustormerInfo(customer);
return customer.getId();
}
转载地址:http://mqjdv.baihongyu.com/