以Customer和Order为例:一个顾客可以有多个订单,一张订单对应一个顾客。并且知道某顾客时就知道其所有的订单,得到一张订单时可以由其知道消费该订单的顾客。
(1)持久化类Customer和Order的关系:
(2)数据库中表的关系:
(3)持久化类:
持久化类Customer | 持久化类Order |
package Bean;
import java.util.Set;
public classCustomer { private Stringname; private Longid; private Set<Order>orders;
public String getName() { return name; } publicvoid setName(String name) { this.name = name; } public Long getId() { returnid; } publicvoid setId(Long id) { this.id = id; } public Set<Order>getOrders() { return orders; } public voidsetOrders(Set<Order> orders) { this.orders = orders; }
}
|
package Bean;
public classOrder { private Longid; private Customercustomer; private StringorderNumber; public Long getId() { returnid; } public voidsetId(Long id) { this.id = id; } public Customer getCustomer() { return customer; } public voidsetCustomer(Customer customer) { this.customer = customer; } public String getOrderNumber() { return orderNumber; } public voidsetOrderNumber(String orderNumber) { this.orderNumber = orderNumber; } }
|
Customer.hbm.xml |
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <classname="Bean.Customer"table="customers"> <idname="id"type="long"> <generatorclass="increment"></generator> </id> <propertyname="name"column="name"type="string"></property> <setname="orders"cascade="save-update"inverse="true"> <keycolumn="customer_id"></key> <one-to-manyclass="Bean.Order"/> </set> </class> </hibernate-mapping> 说明:
•<set>元素包括以下属性: –name: 设定待映射的持久化类的属性名,这里为Customer类的orders属性 –cascade: 当取值为“save-update”,表示级联保存和更新。 –inverse:当取值为“true”,表示在双向关联中,这一端为镜像端。 •<set>元素还包含两个子元素:<key>和<one-to-many>,<one-to-many>元素设定所关联的持久化类,此处为Order类,<key>元素设定与所关联的持久化类对应的表的外键,此处为ORDERS表的CUSTOMER_ID字段。 |
Order.hbm.xml |
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <classname="Bean.Order"table="orders"> <idname="id"type="long"> <generatorclass="increment"></generator> </id> <propertyname="orderNumber"column="order_number"type="string"></property> <many-to-onename="customer"column="customer_id"cascade="all"class="Bean.Customer"> </many-to-one> </class> </hibernate-mapping> 说明:
<many-to-one>元素的属性 •name:设定待映射的持久化类的属性名,此处为Order类的customer属性。 •column:设定和持久化类的属性对应的表的外键,此处为ORDERS表的外键CUSTOMER_ID。 •class:设定持久化类的属性的类型,此处设定customer属性为Customer类型。 |
<mappingresource="Order.hbm.xml"/>
<mappingresource="Customer.hbm.xml"/>
(6)级联保存Customer和Order的对象:package test;
import java.util.HashSet;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import Bean.Customer;
import Bean.Order;
publicclassTestCo {
privatestatic SessionFactoryfactory;
static {
factory =newConfiguration().configure().buildSessionFactory();
}
publicstaticvoid main(String[] args) {
Sessionsession = factory.openSession();
Transactiontx = null;
Customercustomer = null;
try {
tx= session.beginTransaction();
customer= newCustomer();
customer.setName("c1");
customer.setOrders(newHashSet<Order>());
Ordero1 = newOrder();
Ordero2 = newOrder();
o1.setOrderNumber("order1");
o1.setCustomer(customer);
o2.setOrderNumber("order2");
o2.setCustomer(customer);
customer.getOrders().add(o1);
customer.getOrders().add(o2);
session.save(customer);
tx.commit();
}catch(Exception e) {
if (tx !=null)
tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
}
}
说明:建表可以手动使用SQL也可配置hibernate.cfg.xml使Hibernate自动帮我们建好。
可进一步修改映射文件中的lazy、cascade属性设置是否延迟加载、级联的程度。