- 背景:
在上一篇文章中实现双向关联时,其中在Customer.java中我们使用了java.util.List<Order>来关联多的Order。其实还有另外一种实现方法:使用java.util.Set来替代java.util.List。
Customer.java(定义Order:private java.util.Set<Order> orders=new HashSet<Order>();)
package com.dx.hibernate.onetomany; import java.util.HashSet;
import java.util.List;
import java.util.Set; public class Customer {
private Integer customerId;
private String customerName;
private Set<Order> orders = new HashSet<Order>(); public Customer() {
} public Customer(String customerName) {
super();
this.customerName = customerName;
} public Integer getCustomerId() {
return customerId;
} public void setCustomerId(Integer customerId) {
this.customerId = customerId;
} public String getCustomerName() {
return customerName;
} public void setCustomerName(String customerName) {
this.customerName = customerName;
} public Set<Order> getOrders() {
return orders;
} public void setOrders(Set<Order> orders) {
this.orders = orders;
}
}
Customer.hbm.xml
set 节点设置为:
<set name="orders" table="ORDERS" inverse="true" lazy="true">
<key>
<column name="CUSTOMER_ID" />
</key>
<one-to-many class="com.dx.hibernate.onetomany.Order" />
</set>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-6-1 15:27:44 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.dx.hibernate.onetomany.Customer" table="CUSTOMER">
<id name="customerId" type="java.lang.Integer">
<column name="CUSTOMER_ID" />
<generator class="native" />
</id>
<property name="customerName" type="java.lang.String">
<column name="CUSTOMER_NAME" />
</property>
<set name="orders" table="ORDERS" inverse="false" lazy="true">
<key>
<column name="CUSTOMER_ID" />
</key>
<one-to-many class="com.dx.hibernate.onetomany.Order" />
</set>
</class>
</hibernate-mapping>
Order.java
package com.dx.hibernate.onetomany; public class Order {
private Integer orderId;
private String orderName;
private Customer customer; public Order() {
super();
} public Order(String orderName) {
super();
this.orderName = orderName;
} public Integer getOrderId() {
return orderId;
} public void setOrderId(Integer orderId) {
this.orderId = orderId;
} public String getOrderName() {
return orderName;
} public void setOrderName(String orderName) {
this.orderName = orderName;
} public Customer getCustomer() {
return customer;
} public void setCustomer(Customer customer) {
this.customer = customer;
} }
Order.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-6-1 15:27:44 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.dx.hibernate.onetomany.Order" table="ORDERS">
<id name="orderId" type="java.lang.Integer">
<column name="ORDER_ID" />
<generator class="native" />
</id>
<property name="orderName" type="java.lang.String">
<column name="ORDER_NAME" />
</property>
<many-to-one name="customer" class="com.dx.hibernate.onetomany.Customer" fetch="join">
<column name="CUSTOMER_ID" />
</many-to-one>
</class>
</hibernate-mapping>
测试1:
@Test
public void test() {
Order order1 = new Order("order1");
Order order2 = new Order("order2");
Customer customer = new Customer("customer1"); // 当Customer.hbm.xml中set节点属性Inverse为false时。
customer.getOrders().add(order1);
customer.getOrders().add(order2); session.save(customer);
session.save(order1);
session.save(order2); Customer customerFetch = (Customer) session.get(Customer.class, 1); System.out.println(customerFetch.getCustomerName());
System.out.println(customerFetch.getOrders().size());
System.out.println(customerFetch.getOrders().getClass()); }
测试通过。
在运行过程中Customer.java中Set为:class org.hibernate.collection.internal.PersistentSet,属于hibernate的一个代理对象。
session关闭,将抛出异常。
测试2:
修改Customer.hbm.xml中set节点属性inverse="true"
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-6-1 15:27:44 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.dx.hibernate.onetomany.Customer" table="CUSTOMER">
<id name="customerId" type="java.lang.Integer">
<column name="CUSTOMER_ID" />
<generator class="native" />
</id>
<property name="customerName" type="java.lang.String">
<column name="CUSTOMER_NAME" />
</property>
<set name="orders" table="ORDERS" inverse="true" lazy="true">
<key>
<column name="CUSTOMER_ID" />
</key>
<one-to-many class="com.dx.hibernate.onetomany.Order" />
</set>
</class>
</hibernate-mapping>
测试代码:
@Test
public void test() {
Order order1 = new Order("order1");
Order order2 = new Order("order2");
Customer customer = new Customer("customer1"); // 当Customer.hbm.xml中set节点属性inverse为false时。
// customer.getOrders().add(order1);
// customer.getOrders().add(order2); // 当Customer.hbm.xml中set节点属性inverse为true时。
order1.setCustomer(customer);
order2.setCustomer(customer); session.save(customer);
session.save(order1);
session.save(order2); Customer customerFetch = (Customer) session.get(Customer.class, 1); System.out.println(customerFetch.getCustomerName());
System.out.println(customerFetch.getOrders().size());
System.out.println(customerFetch.getOrders().getClass()); }
测试通过。
接下来我们要将的是set节点的三个重要属性:inverse,cascade,order-by。
- inverse属性
1)在Hibernate中通过对inverse属性来决定是有双向关联的哪一方来维护表和表之间的关系。inverse=false为主动方,inverse=true的为被动方,由主动方负责维护关联关系。
2)在没有设置inverse=true的情况下,父子两边都维护父子关系。
3)在1-n关系中,将n方设为主控方将有助于性能改善。
- cascade属性
级联,每个Hibernate session的基本操作包括persist()、merge()、saveOrUpdate()、delete()、lock()、refresh()、evict()、replicate(),这些操作都有对应的级联风格(cascade style)。这些级联风格(cascade style)风格分别命名为persist、merge、save-update、delete、lock、refresh、evict、replicate。
级联风格 | Session中的方法 |
---|---|
persist | persist() |
merge | merge() |
save-update | save()、update()、saveOrUpdate() |
delete | delete() |
lock | lock() |
refresh | refresh() |
evict | evict() |
replicate | replicate() |
注意:
一般默认是不建议设置cascade属性。
- order-by属性
设置查询时,排序方式。
测试1:
修改Customer.hbml.xml
<set name="orders" table="ORDERS" inverse="true" lazy="true" cascade="save-update,delete" order-by="CUSTOMER_ID desc">
<key>
<column name="CUSTOMER_ID" />
</key>
<one-to-many class="com.dx.hibernate.onetomany.Order" />
</set>
修改test代码:
@Test
public void test() {
Order order1 = new Order("order1");
Order order2 = new Order("order2");
Customer customer = new Customer("customer3"); customer.getOrders().add(order1);
customer.getOrders().add(order2);
order1.setCustomer(customer);
order2.setCustomer(customer); session.save(customer);
// session.save(order1);
// session.save(order2);
} @Test
public void testRemoveCustomer() {
Customer customer = (Customer) session.get(Customer.class, 3);
session.remove(customer);
}
测试通过。