hibernate框架一对多级联删除例子(十三)
2017-11-19 16:13
1176 查看
一、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会先把外键的约束删除掉,然后再删除客户,联系人没有删除。
二、hibernate级联删除——一级联多
2.1 配置级联删除
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级联删除——多级联一——多方配置
4000
3.1 配置级联
1 取消customer的级联2. linkman添加级联删除
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配置linkman配置
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框架一对多级联保存例子(十二)
- 关于hibernate删除对象不级联的问题
- java框架整合例子(spring、spring mvc、spring data jpa、hibernate)
- hibernate 级联删除的问题
- 关于如何实现在整合struts2和hibernate4框架下对many-to-one级联数据的存储
- 再谈Hibernate级联删除——JPA下的Hibernate实现一对多级联删除CascadeType.DELETE_ORPHAN
- Spring+struts2+hibernate框架的Java Web项目利用POI导出Excel简单例子
- hibernate OnetoOne 级联删除
- hibernate级联操 同时向2张表插入数据(一对多的关联,一个简单的例子说明)
- Hibernate 级联删除
- Hibernate级联保存与删除
- Hibernate 级联删除异常 deleted object would be re-saved by cascade
- hibernate 自身级联删除
- hibernate级联操 同时向2张表插入数据(一对多的关联,一个简单的例子说明)
- hibernate 一对多 级联 保存修改 删除
- 4000 hibernate框架多对多-级联保存(十八)
- hibernate - 解决表结构外键限定为非空,级联保存和级联删除抛异常的问题
- Hibernate注解中的manytomany级联与不级联删除问题的解决
- ofbiz框架入门例子详解-------删除篇
- JS框架avalon简单例子 行编辑 添加 修改 删除 验证