hibernate框架一对多级联删除例子(十三)

一、hibernate不级联删除

	/**
	 * 不级联删除:删除客户,客户下有2个联系人
	 */
	@Test
	public void run6() {
		Session session = HibernateUtils.getCurrentSession();
		Transaction tx = session.beginTransaction();

		Customer c1 = session.get(Customer.class, 1L);
		session.delete(c1);

		tx.commit();
	}
hibernate框架一对多级联删除例子(十三)_第1张图片

hibernate会先把外键的约束删除掉,然后再删除客户,联系人没有删除。

二、hibernate级联删除——一级联多

2.1 配置级联删除

hibernate框架一对多级联删除例子(十三)_第2张图片

2.2 测试代码

	/**
	 * 级联删除:删除客户,级联删除客户下的联系人
	 */
	@Test
	public void run7() {
		Session session = HibernateUtils.getCurrentSession();
		Transaction tx = session.beginTransaction();

		Customer c1 = session.get(Customer.class, 1L);
		session.delete(c1);

		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
        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=?
Hibernate: 
    delete 
    from
        cst_linkman 
    where
        lkm_id=?
Hibernate: 
    delete 
    from
        cst_linkman 
    where
        lkm_id=?
Hibernate: 
    delete 
    from
        cst_customer 
    where
        cust_id=?

三、hibernate级联删除——多级联一——多方配置

3.1 配置级联

1 取消customer的级联

hibernate框架一对多级联删除例子(十三)_第3张图片

2. linkman添加级联删除

hibernate框架一对多级联删除例子(十三)_第4张图片

3.2 测试代码

	/**
	 * 级联删除:删除联系人,级联删除客户
	 */
	@Test
	public void run8() {
		Session session = HibernateUtils.getCurrentSession();
		Transaction tx = session.beginTransaction();

		Linkman l1 = session.get(Linkman.class, 1L);
		session.delete(l1);

		tx.commit();
	}

3.3 运行结果

第一个linkman删掉了,customer删掉了,第二个linkman没有删掉

看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
        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
        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: 
    update
        cst_linkman 
    set
        lkm_cust_id=null 
    where
        lkm_cust_id=?
Hibernate: 
    delete 
    from
        cst_linkman 
    where
        lkm_id=?
Hibernate: 
    delete 
    from
        cst_customer 
    where
        cust_id=?

四、hibernate级联删除——多级联一——一方和多方都配置

4.1 配置级联

customer配置

hibernate框架一对多级联删除例子(十三)_第5张图片

linkman配置

hibernate框架一对多级联删除例子(十三)_第6张图片

4.2 测试代码

	/**
	 * 级联删除:删除联系人,级联删除客户
	 */
	@Test
	public void run9() {
		Session session = HibernateUtils.getCurrentSession();
		Transaction tx = session.beginTransaction();

		Linkman l1 = session.get(Linkman.class, 1L);
		session.delete(l1);

		tx.commit();
	}
	

4.3 运行结果

customer和linkman都删除了

看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
        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
        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
        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=?
Hibernate: 
    delete 
    from
        cst_linkman 
    where
        lkm_id=?
Hibernate: 
    delete 
    from
        cst_linkman 
    where
        lkm_id=?
Hibernate: 
    delete 
    from
        cst_customer 
    where
        cust_id=?


你可能感兴趣的:(Hibernate)