继上篇博客之后的补充:双向的一对多与多对一级联关系操作。具体请看下面案例分析:
配置文件没有什么变化,就是上一篇博客中的多对一与一对多的集合一块,不会的留言,这里不再细说了。
主要测试类,下面类测试级联更新,级联删除的多种情况,级联插入,级联查找的不同情况,并附有相关注释介绍相关操作
package com.hbsi.orders_o2mboth;
import java.util.Set;
import org.hibernate.Session;
import org.junit.Test;
import com.hbsi.utils.HibernateUtil;
public class One2ManyBoth {
Session session = null;
@Test
public void add(){
try{
session = HibernateUtil.getSession();
session.beginTransaction();
Customer cus = new Customer("红军1","母",85);
Orders ord1 = new Orders("餐巾纸1",3.5);
Orders ord2 = new Orders("鼠标垫1",10.3);
ord1.setCus(cus);
ord2.setCus(cus);
/*Set<Orders> ords = new HashSet<Orders>();
ords.add(ord1);
ords.add(ord2);
session.save(cus);*/
session.save(ord1);
session.save(ord2);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}finally{
//HibernateUtil.close();
}
}
//修改订单所有者
@Test
public void change(){
try{
session = HibernateUtil.getSession();
session.beginTransaction();
//获取订单名
Orders order = (Orders) session.get(Orders.class, 15);
//获取客户名
Customer custmer = (Customer)session.get(Customer.class,6);
//把获取到订单放到获取到的客户上,达到修改订单所有者的目的
order.setCus(custmer);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateUtil.close();
}
}
//删除某一订单,单多方xxx.hbm.xml中都没有设置级联删除
@Test
public void delete1(){
try{
session = HibernateUtil.getSession();
session.beginTransaction();
//获取订单名
Orders order = (Orders) session.get(Orders.class, 23);
session.delete(order);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateUtil.close();
}
}
//删除某一订单和与之级联的客户,并把剩下级联订单的外键设置为空,Orders.hbm.xml中设置级联删除,Customer.hbm.xml中不设置级联操作也不设置放弃操作,即inverse=“true”
@Test
public void delete2(){
try{
session = HibernateUtil.getSession();
session.beginTransaction();
//获取订单名
Orders order = (Orders) session.get(Orders.class, 25);
session.delete(order);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateUtil.close();
}
}
//删除某一客户,但是其下的订单可以不删除,但是会自动更新为空。Customer.hbm.xml下没有设置级联删除,如果Customer.hbm.xml中设置了放弃操作会报错
@Test
public void delete3(){
try{
session = HibernateUtil.getSession();
session.beginTransaction();
//获取订单名
Customer cus = (Customer) session.get(Customer.class, 14);
session.delete(cus);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateUtil.close();
}
}
//删除某一客户,级联删除,连带着其下的所有订单全部删除.Customer.hbm.xml下设置级联删除,如果Customer.hbm.xml中设置了放弃操作会报错
@Test
public void delete4(){
try{
session = HibernateUtil.getSession();
session.beginTransaction();
//获取订单名
Customer cus = (Customer) session.get(Customer.class, 15);
session.delete(cus);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateUtil.close();
}
}
//查询某一客户的所有订单号
@Test
public void find1(){
try{
session = HibernateUtil.getSession();
Customer cus = (Customer) session.get(Customer.class, 6);
Set<Orders> ords = cus.getOrd();
for(Orders ord : ords){
System.out.println(ord.getOname());
}
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateUtil.close();
}
}
//查询某一订单的所属客户
@Test
public void find2(){
try{
session = HibernateUtil.getSession();
Orders ord = (Orders) session.get(Orders.class,13);
System.out.println(ord.getCus().getCname());
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateUtil.close();
}
}
}