hibernate框架一对多cascade取值和孤儿删除(十四)

来源:互联网 发布:windows db2 编辑:程序博客网 时间:2024/05/16 08:52

1. 需要掌握的取值如下
* none -- 不使用级联
* save-update -- 级联保存或更新
* delete -- 级联删除
* delete-orphan-- 孤儿删除.(注意:只能应用在一对多关系)
* all -- 除了delete-orphan的所有情况.(包含save-update delete)
* all-delete-orphan-- 包含了delete-orphan的所有情况.(包含save-update delete delete-orphan)
2. 孤儿删除(孤子删除),只有在一对多的环境下才有孤儿删除
* 在一对多的关系中,可以将一的一方认为是父方.将多的一方认为是子方.孤儿删除:在解除了父子关系的时候.将子方记录就直接删除。
* <many-to-one cascade="delete-orphan" />

一、解除关系

把一号linkman和customer解除关系

只需要把一号linkman从customer的集合中移除

1.1 测试代码

/** * 解除关系:从集合中删除联系人 */@Testpublic void run10() {Session session = HibernateUtils.getCurrentSession();Transaction tx = session.beginTransaction();// 先获取客户Customer c1 = session.get(Customer.class, 1L);// 获取1号联系人Linkman l1 = session.get(Linkman.class, 1L);// 解除c1.getLinkmans().remove(l1);tx.commit();}

1.2 运行结果

一号联系人的外键变成null了


看sql:

log4j:WARN No appenders could be found for logger (org.jboss.logging).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Hibernate:     select        customer0_.cust_id as cust_id1_0_0_,        customer0_.cust_name as cust_nam2_0_0_,        customer0_.cust_user_id as cust_use3_0_0_,        customer0_.cust_create_id as cust_cre4_0_0_,        customer0_.cust_source as cust_sou5_0_0_,        customer0_.cust_industry as cust_ind6_0_0_,        customer0_.cust_level as cust_lev7_0_0_,        customer0_.cust_linkman as cust_lin8_0_0_,        customer0_.cust_phone as cust_pho9_0_0_,        customer0_.cust_mobile as cust_mo10_0_0_     from        cst_customer customer0_     where        customer0_.cust_id=?Hibernate:     select        linkman0_.lkm_id as lkm_id1_1_0_,        linkman0_.lkm_name as lkm_name2_1_0_,        linkman0_.lkm_gender as lkm_gend3_1_0_,        linkman0_.lkm_phone as lkm_phon4_1_0_,        linkman0_.lkm_mobile as lkm_mobi5_1_0_,        linkman0_.lkm_email as lkm_emai6_1_0_,        linkman0_.lkm_qq as lkm_qq7_1_0_,        linkman0_.lkm_position as lkm_posi8_1_0_,        linkman0_.lkm_memo as lkm_memo9_1_0_,        linkman0_.lkm_cust_id as lkm_cus10_1_0_     from        cst_linkman linkman0_     where        linkman0_.lkm_id=?Hibernate:     select        linkmans0_.lkm_cust_id as lkm_cus10_1_0_,        linkmans0_.lkm_id as lkm_id1_1_0_,        linkmans0_.lkm_id as lkm_id1_1_1_,        linkmans0_.lkm_name as lkm_name2_1_1_,        linkmans0_.lkm_gender as lkm_gend3_1_1_,        linkmans0_.lkm_phone as lkm_phon4_1_1_,        linkmans0_.lkm_mobile as lkm_mobi5_1_1_,        linkmans0_.lkm_email as lkm_emai6_1_1_,        linkmans0_.lkm_qq as lkm_qq7_1_1_,        linkmans0_.lkm_position as lkm_posi8_1_1_,        linkmans0_.lkm_memo as lkm_memo9_1_1_,        linkmans0_.lkm_cust_id as lkm_cus10_1_1_     from        cst_linkman linkmans0_     where        linkmans0_.lkm_cust_id=?Hibernate:     update        cst_linkman     set        lkm_cust_id=null     where        lkm_cust_id=?         and lkm_id=?

二、孤儿删除

孤儿删除:就是在解除关系的时候,把这条记录给删掉

2.1 配置cascade

在customer中配置cascade="delete-orphan"


达到

2.2 测试代码

@Testpublic void run10() {Session session = HibernateUtils.getCurrentSession();Transaction tx = session.beginTransaction();// 先获取客户Customer c1 = session.get(Customer.class, 1L);// 获取1号联系人Linkman l1 = session.get(Linkman.class, 1L);// 解除c1.getLinkmans().remove(l1);tx.commit();}

2.3 运行结果

一号联系人被删除了


看sql:

log4j:WARN No appenders could be found for logger (org.jboss.logging).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Hibernate:     select        customer0_.cust_id as cust_id1_0_0_,        customer0_.cust_name as cust_nam2_0_0_,        customer0_.cust_user_id as cust_use3_0_0_,        customer0_.cust_create_id as cust_cre4_0_0_,        customer0_.cust_source as cust_sou5_0_0_,        customer0_.cust_industry as cust_ind6_0_0_,        customer0_.cust_level as cust_lev7_0_0_,        customer0_.cust_linkman as cust_lin8_0_0_,        customer0_.cust_phone as cust_pho9_0_0_,        customer0_.cust_mobile as cust_mo10_0_0_     from        cst_customer customer0_     where        customer0_.cust_id=?Hibernate:     select        linkman0_.lkm_id as lkm_id1_1_0_,        linkman0_.lkm_name as lkm_name2_1_0_,        linkman0_.lkm_gender as lkm_gend3_1_0_,        linkman0_.lkm_phone as lkm_phon4_1_0_,        linkman0_.lkm_mobile as lkm_mobi5_1_0_,        linkman0_.lkm_email as lkm_emai6_1_0_,        linkman0_.lkm_qq as lkm_qq7_1_0_,        linkman0_.lkm_position as lkm_posi8_1_0_,        linkman0_.lkm_memo as lkm_memo9_1_0_,        linkman0_.lkm_cust_id as lkm_cus10_1_0_     from        cst_linkman linkman0_     where        linkman0_.lkm_id=?Hibernate:     select        linkmans0_.lkm_cust_id as lkm_cus10_1_0_,        linkmans0_.lkm_id as lkm_id1_1_0_,        linkmans0_.lkm_id as lkm_id1_1_1_,        linkmans0_.lkm_name as lkm_name2_1_1_,        linkmans0_.lkm_gender as lkm_gend3_1_1_,        linkmans0_.lkm_phone as lkm_phon4_1_1_,        linkmans0_.lkm_mobile as lkm_mobi5_1_1_,        linkmans0_.lkm_email as lkm_emai6_1_1_,        linkmans0_.lkm_qq as lkm_qq7_1_1_,        linkmans0_.lkm_position as lkm_posi8_1_1_,        linkmans0_.lkm_memo as lkm_memo9_1_1_,        linkmans0_.lkm_cust_id as lkm_cus10_1_1_     from        cst_linkman linkmans0_     where        linkmans0_.lkm_cust_id=?Hibernate:     update        cst_linkman     set        lkm_cust_id=null     where        lkm_cust_id=?         and lkm_id=?Hibernate:     delete     from        cst_linkman     where        lkm_id=?



阅读全文
0 0
原创粉丝点击