HIbernate级联删除(hibernate 双向一对多中 主表设置cascade="all-delete-orphan",在save时出现

数据库为:Oracle 9i

主表为: 用户表(member)
create table member (
member_id number(10,0) not null,
name varchar2(10 char),
sex varchar2(1 char),
primary key (member_id)
)

从表为: 订单表(orders)
create table orders (
Order_id number(10,0) not null,
total_price float,
member_id number(10,0),
primary key (Order_id)
)
alter table orders
add constraint FKC3DF62E5CDAA53AA
foreign key (member_id)
references member

双向关联映射细分:

在主表member的关系映射文件中:
1)一端(member)中对多端(order)采取了延迟加载策略:lazy="true"默认
2)将多端(order)的传播持久性(级联)设置为最全面的全部级联(包括孤儿模式):cascade="all-delete-orphan"
3)将维护关系的控制权交给多端(order):inverse="true"

在从表orders的关系映射文件中:
1)多端(order)对一端(member)采取了预先抓取策略:fetch="join"(并且要把一端‘member’映射文件的class标签的lazy属性设置为false)
2)将一端(member)的传播持久性(级联)设置为存储、更新:cascade="save-update"


用户表member(主表):
<hibernate-mapping>
<class name="hbp.ch04.ex03.model.Member" table="member" lazy="false">
<id name="memberId" column="member_id" type="java.lang.Integer">
<generator class="native"> </generator>
</id>
<property name="name" column="name" type="java.lang.String" length="10"> </property>
<property name="sex" column="sex" type="java.lang.String" length="1"> </property>
<set name="orders" order-by="order_id" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="member_id"> </key>
<one-to-many class="hbp.ch04.ex03.model.Order"/>
</set>
</class>
</hibernate-mapping>

订单表orders(从表)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="hbp.ch04.ex03.model.Order" table="orders">
<id name="orderId" column="Order_id" type="java.lang.Integer">
<generator class="native"> </generator>
</id>
<property name="totalPrice" column="total_price" type="java.lang.Float"> </property>
<many-to-one name="member" column="member_id" class="hbp.ch04.ex03.model.Member" cascade="save-update" fetch="join"> </many-to-one>
</class>
</hibernate-mapping>

pojo类:

Member------------------------

import java.util.HashSet;
import java.util.Set;

public class Member {

// Fields

private Integer memberId;
private String name;
private String sex;
private Set orders;

// Constructors
/** default constructor */

public Member() {
// TODO Auto-generated constructor stub
}

// Property accessors

/**
* @return the orders
*/
public Set getOrders() {
if (orders == null) {
orders = new HashSet();
}
return orders;
}
// 其他 get/set访问方法省略。。。。。
}

Order-----------------
public class Order {

// Fields


}

Member的DAO类(DAO实现的接口省略。。。)

import org.hibernate.*;
import util.HibernateSessionFactory;

import hbp.dao.MemberDAO;
import hbp.model.Member;
import hbp.model.Order;

public class HibernateMemberDAO implements MemberDAO {

public void deleteMember(Integer memberId) {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();

Member member = (Member)session.get(Member.class, memberId);
session.delete(member);
session.getTransaction().commit();
session.close();

}

public Member getMemberById(Integer memberId) {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();

Member member = (Member)session.get(Member.class, memberId);
//Hibernate.initialize(member.getOrders());

session.getTransaction().commit();
return member;
}

public Order getOrderById(Integer orderId) {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();

Order order=(Order)session.get(Order.class, orderId);
//Hibernate.initialize(order.getMember());
session.getTransaction().commit();
return order;
}

public void saveMember(Member member) {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();

session.save(member);

session.getTransaction().commit();

}

public void saveOrder(Integer memberId, Order order) {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();

Member member = (Member)session.get(Member.class, memberId);
order.setMember(member);
member.getOrders().add(order);

session.save(member);

session.getTransaction().commit();


}

public void delete_orphan(Member member,Order order) {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();

member.getOrders().remove(order);

session.save(member);

session.getTransaction().commit();

}
}


测试方法:
import java.util.HashSet;

import hbp.dao.hibernate.HibernateMemberDAO;
import hbp.model.Member;
import hbp.model.Order;
import junit.framework.TestCase;

public class HibernateMemberDAO03Test extends TestCase {

public void testSaveMember(){
HibernateMemberDAO memberDAO = new HibernateMemberDAO();

Member member, member2, member3;
member = new Member();
member.setName("李四");
member.setSex("f");
member.setOrders(new HashSet());
memberDAO.saveMember(member);

member2 = new Member();
member2.setName("王五");
member2.setSex("m");
member2.setOrders(new HashSet());
memberDAO.saveMember(member2);

member3 = new Member();
member3.setName("张三");
member3.setSex("m");
member3.setOrders(new HashSet());
memberDAO.saveMember(member3);
}

public void testSaveOrder() {
HibernateMemberDAO memberDAO = new HibernateMemberDAO();
Order order = new Order();
order.setTotalPrice(new Float(12.34));
memberDAO.saveOrder(new Integer(3), order);

}

public void testGetMemberById() {
HibernateMemberDAO memberDAO = new HibernateMemberDAO();
Member member = memberDAO.getMemberById(new Integer(3));
System.out.println("会员姓名:" + member.getName());
System.out.println("订单数量:" + member.getOrders().size());
}

public void testGetOrderById() {
HibernateMemberDAO memberDAO = new HibernateMemberDAO();
Order order = memberDAO.getOrderById(new Integer(4));
System.out.println("会员姓名:" + order.getMember().getName());
System.out.println("订单价格:" + order.getTotalPrice());
}




public void testDeleteMember() {
/*
HibernateMemberDAO memberDAO = new HibernateMemberDAO();
memberDAO.deleteMember(new Integer(2));
memberDAO.deleteMember(new Integer(3));
*/

HibernateMemberDAO memberDAO = new HibernateMemberDAO();
Member member = memberDAO.getMemberById(new Integer(3));
Order order = memberDAO.getOrderById(new Integer(4));
memberDAO.delete_orphan(member, order);

}

}

http://hi.baidu.com/farmer521/blog/item/efb87e86a70a342dc65cc3ca.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值