domain:
Customer:
package com.itheima.domain;
import java.util.HashSet;
import java.util.Set;
public class Customer {
private Integer id;
private String name;
//在1 的一方,表达持有多的一方的引用=>使用集合
private Set orders = new HashSet();
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 getOrders() {
return orders;
}
public void setOrders(Set orders) {
this.orders = orders;
}
}
Order:
package com.itheima.domain;
public class Order {
private Integer id;
private String name;
private Customer customer;
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 Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
Customer.hbm.xml:
Order.hbm.xml:
hibernate.cfg.xml:
com.mysql.jdbc.Driver
root
1234
jdbc:mysql://localhost:3306/EE19Day02
true
true
update
org.hibernate.dialect.MySQLDialect
thread
package com.itheima.e_one2many;
import java.util.Set;
import org.hibernate.Session;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.domain.Order;
import com.itheima.utils.HibernateUtils;
//测试 一对多关系
public class Demo1 {
@Test
//1 测试1对多关系中,保存操作
//共打印5条语句
//前3条打印insert => 保存对象,维护外键
//后两条打印update => 维护外键
//解决=> 单纯指定 关系由其中一方来维护.另一方不维护关系.
//注意=> 外键维护的放弃,只能由非外键所在对象来放弃.
//Customer inverse属性: true
//只打印3条语句=> 外键由order自己来维护
public void fun1(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Customer c = new Customer();
c.setName("tom");
Order o1 = new Order();
o1.setName("肥皂");
Order o2 = new Order();
o2.setName("蜡烛");
//c.getOrders().add(o1);//维护关系
//c.getOrders().add(o2); //维护关系
o1.setCustomer(c);//维护关系
o2.setCustomer(c);//维护关系
session.save(c);//保存对象
session.save(o1);//保存对象
session.save(o2);//保存对象
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
//多表关系=> 删除
//删除 用户时 ,会先移除 Customer中引用的外键.然后再删除Customer
// 结论: 维护一方的对象时,会自动维护另一方的关系
// Customer 的 inverse属性: true
// 会报错 => Customer不负责维护外键, 直接删除Customer 会导致,order引用了无效的id.违反了外键约束.
@Test
public void fun2(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Customer c = (Customer) session.get(Customer.class, 3);
// Customer 的 inverse属性: true
Set set = c.getOrders();
for(Order o : set){
o.setCustomer(null);//设置订单不属于任何Customer
}
session.delete(c);
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
//什么时候配置inverse属性?
// 主要看业务. 如果一的一方经常需要维护外键 = 在1的一方不要配置inverse属性.
}
级联保存和修改:
package com.itheima.e_one2many;
import java.util.Set;
import org.hibernate.Session;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.domain.Order;
import com.itheima.utils.HibernateUtils;
//测试 一对多关系 级联保存 级联修改
public class Demo2 {
@Test
//增
//我们希望在保存Customer时,自动将未保存的Orders当中的Order保存
//cascade: save-update
public void fun1(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Customer c = new Customer();
c.setName("tom");
Order o1 = new Order();
o1.setName("肥皂");
Order o2 = new Order();
o2.setName("蜡烛");
c.getOrders().add(o1);//维护关系
c.getOrders().add(o2); //维护关系
/*
o1.setCustomer(c);//维护关系
o2.setCustomer(c);//维护关系
*/
session.save(c);//保存对象
//session.save(o1);//保存对象
//session.save(o2);//保存对象
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
//增
//我们希望在保存Customer时,自动将未保存的Orders当中的Order保存
//cascade: save-update
public void fun2(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Customer c = (Customer) session.get(Customer.class, 8);//1条 select
for(Order o :c.getOrders()){ // 1条 select
o.setName("哇哈哈"); // 修改订单
}
//------------------------------------------------
session.getTransaction().commit();//因为设置级联修改,自动将订单的修改保存到数据
//update语句
session.close(); // 游离状态
}
@Test
//cascade: delete
//删除Customer时 ,会将Customer下的订单一并删除
//inverse : false 6条sql语句
//inverse : true 5条sql语句 比上面少一条维护外键
public void fun3(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Customer c = (Customer) session.get(Customer.class, 7);//1条 select
session.delete(c);//删除Customer
// 删除两个Order
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
//cascade: delete
//操作的两方cascade值都为delete
//需要注意: 千万不要在两方都配置 级联删除. 删除任何一方,会导致整个关系链对象全部删除.
public void fun4(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Order o = (Order) session.get(Order.class, 9);//select
session.delete(o);//delete删除当前order
//找到所有关联的Customer删除 select
// delete Customer
// Customer配置了级联删除=> select 找下面的order
// 删除所有Order
//删除Customer
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
}
级联删除:孤儿删除:
package com.itheima.e_one2many;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.domain.Order;
import com.itheima.utils.HibernateUtils;
//测试 一对多关系
public class Demo3 {
@Test
//inverse:false
//cascade: delete-orphan 孤儿删除 => 当没有任何外键引用Order时,order 会被删除
public void fun1(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Customer c = (Customer) session.get(Customer.class, 9);
Iterator it = c.getOrders().iterator();
//注意: 删除Customer下的订单时,不能使用 c.setOrders(null); c.setOrders(new HashSet());
while(it.hasNext()){ // 遍历Customer下的订单,并将订单删除 => 维护关系
it.next();
it.remove();
}
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
}
级联删除:整合:
package com.itheima.e_one2many;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.domain.Order;
import com.itheima.utils.HibernateUtils;
//测试 一对多关系
public class Demo4 {
@Test
//cascade: all-delete-orphan => 相当于配置 save-update,delete,delete-orphan
public void fun1(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Customer c = new Customer();
c.setName("tom");
Order o1 = new Order();
o1.setName("肥皂");
Order o2 = new Order();
o2.setName("蜡烛");
c.getOrders().add(o1);//维护关系
c.getOrders().add(o2); //维护关系
session.save(c);
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
//cascade: all-delete-orphan => 相当于配置 save-update,delete,delete-orphan
public void fun2(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Customer c = (Customer) session.get(Customer.class, 10);
session.delete(c);
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
@Test
//cascade: all-delete-orphan => 相当于配置 save-update,delete,delete-orphan
public void fun3(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
//------------------------------------------------
Customer c = (Customer) session.get(Customer.class, 12);
Iterator it = c.getOrders().iterator();
while(it.hasNext()){ // 遍历Customer下的订单,并将订单删除 => 维护关系
it.next();
it.remove();
}
//------------------------------------------------
session.getTransaction().commit();
session.close(); // 游离状态
}
}