/**
* 不级联删除:删除客户,客户下有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会先把外键的约束删除掉,然后再删除客户,联系人没有删除。
/**
* 级联删除:删除客户,级联删除客户下的联系人
*/
@Test
public void run7() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Customer c1 = session.get(Customer.class, 1L);
session.delete(c1);
tx.commit();
}
看打印的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=?
1 取消customer的级联
2. linkman添加级联删除
/**
* 级联删除:删除联系人,级联删除客户
*/
@Test
public void run8() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Linkman l1 = session.get(Linkman.class, 1L);
session.delete(l1);
tx.commit();
}
第一个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=?
customer配置
linkman配置
/**
* 级联删除:删除联系人,级联删除客户
*/
@Test
public void run9() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Linkman l1 = session.get(Linkman.class, 1L);
session.delete(l1);
tx.commit();
}
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=?