1对多
实例:客户–订单
实例分析:
代码实现:
Customer.java :
package com.my.bean;
import java.util.HashSet;
import java.util.Set;
public class Customer {
private Integer id;
private String name;
//一个用户可以拥有【多个订单】 (1对多)
private Set<Order> orderSet = new HashSet<Order>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Order> getOrderSet() {
return orderSet;
}
public void setOrderSet(Set<Order> orderSet) {
this.orderSet = orderSet;
}
}
Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.my.bean.Customer" table="t_customer">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<!-- 关联关系:1对多,确定所使用的集合。确定外键名称 。集合中存放数据的类型-->
<set name="orderSet" cascade="delete-orphan"> <!-- inverse="true" lazy="false" -->
<key column="customer_id"></key>
<one-to-many class="com.my.bean.Order"/>
</set>
</class>
</hibernate-mapping>
package com.my.bean;
public class Order {
private Integer id;
private Double price;
//多个订单属于一个用户 (多对1) , 当前订单属于哪一个客户
private Customer customer;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
Order.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.my.bean.Order" table="t_order">
<id name="id">
<generator class="native"></generator>
</id>
<property name="price"></property>
<!-- 关联关系:多对1 。 实现类的全限定类名,外键名称 -->
<many-to-one name="customer" class="com.my.bean.Customer" column="customer_id"></many-to-one>
</class>
</hibernate-mapping>
测试类:
package com.my.bean;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class TestOneToMany {
private SessionFactory factory = new Configuration()
.configure()
.addClass(Customer.class)
.addClass(Order.class)
.buildSessionFactory();
@Test
public void demo01(){
//创建表
Session session = factory.openSession();
session.close();
}
@Test
public void demo02(){
//保存数据 -- 只保存客户
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Customer customer = new Customer();
customer.setName("jack");
session.save(customer);
transaction.commit();
session.close();
}
@Test
public void demo03(){
//保存数据 -- 只保存订单
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Order order = new Order();
order.setPrice(120d);
session.save(order);
transaction.commit();
session.close();
}
@Test
public void demo04(){
//保存数据 -- 客户关联订单,保存客户
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Customer customer = new Customer();
customer.setName("rose");
Order order = new Order();
order.setPrice(120d);
//添加--关联关系
customer.getOrderSet().add(order); //关联的订单没有保存
session.save(order);
session.save(customer);
transaction.commit();
session.close();
}
@Test
public void demo05(){
//保存数据 -- 客户关联订单,保存客户
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Customer customer = new Customer();
customer.setName("rose");
Order order = new Order();
order.setPrice(120d);
//添加--关联关系
customer.getOrderSet().add(order); //关联的订单没有保存
session.save(customer);
session.save(order);
transaction.commit();
session.close();
}
@Test
public void demo06(){
/* 保存数据 -- 客户关联订单,订单管理客户,保存客户和订单
* Order 类必须维护外键的值
* Customer 辅助维护外键信息
* 可以放权,在Customer.hbm.xml文件添加 <set name="orderSet" inverse="true">
*/
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Customer customer = new Customer();
customer.setName("rose");
Order order = new Order();
order.setPrice(120d);
//添加--关联关系
customer.getOrderSet().add(order); // update -- inverse="true" 放权不再维护
order.setCustomer(customer); // update (order需要自己维护关系,先找customer帮着维护)
session.save(order);
session.save(customer);
transaction.commit();
session.close();
}
@Test
public void demo07(){
//保存数据 -- 客户关联订单,订单管理客户,保存客户和订单
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Customer customer = new Customer();
customer.setName("rose");
Order order = new Order();
order.setPrice(120d);
//添加--关联关系
customer.getOrderSet().add(order);
order.setCustomer(customer);
session.save(customer);
session.save(order);
transaction.commit();
session.close();
}
@Test
public void demo08(){
//demo09 准备数据
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Customer customer = new Customer();
customer.setName("rose");
session.save(customer);
for(int i = 0 ; i < 5 ; i ++){
Order order = new Order();
order.setPrice(120d + i);
order.setCustomer(customer);
session.save(order);
}
transaction.commit();
session.close();
}
@Test
public void demo09(){
/*查询数据---延迟
* 配置Customer.hbm.xml取消延迟:<set name="orderSet" inverse="true" lazy="false">
*/
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class, 1);
Set<Order> orderSet = customer.getOrderSet(); //获得数据时将会"延迟加载[懒加载]"数据,在使用时将查询数据
for(Order order : orderSet){
System.out.println(order.getPrice());
}
transaction.commit();
session.close();
}
@Test
public void demo10(){
/* 没有其他配置,例如:inverse="true"
* 默认:将外键的值设置成null
* 级联关系:孤儿删除,如果需要将外键设置成null,直接删除该数据
*/
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class, 1);
Order order = (Order) session.get(Order.class, 5);
//取消 订单,与客户 关联
customer.getOrderSet().remove(order);
transaction.commit();
session.close();
}
}
注意:
1 如果保存数据,客户关联订单,但只保存客户,将出现异常。解决办法同时保存订单
2 inverse=”true” 在1对多方(主表)可以放弃对关联关系维护权利。解决双向关联中多余的sql语句的产生。
3 lazy=”false” 可以配置在查询Customer时,与此同时查询关联集合中的数据
4 移除关联关系时,1对多默认执行update语句,将外键信息更新成null,可以使用“级联关系:孤儿删除”将外键设置成null的数据,直接删除
customer.getOrderSet().remove(order);
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/12087.html