hibernate框架cascade和inverse的区别(十六)

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:35   2151   0

1. cascade用来级联操作(保存、修改和删除)

2. inverse用来维护外键的

一、只配置cascade="save-update"

1.1 配置

只在customer.hbm.xml中配置cascade="save-update"


1.2 测试代码

 /**
  * cascade和inverse的区别
  */
 @Test
 public void run12() {
  Session session = HibernateUtils.getCurrentSession();
  Transaction tx = session.beginTransaction();

  // 级联保存
  Customer c1 = new Customer();
  c1.setCust_name("meimei");

  // 创建2个联系人
  Linkman l1 = new Linkman();
  l1.setLkm_name("xiongda");
  Linkman l2 = new Linkman();
  l2.setLkm_name("xionger");

  // 单向关联
  c1.getLinkmans().add(l1);
  c1.getLinkmans().add(l2);

  // 保存数据
  session.save(c1);

  tx.commit();
 }

1.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: 
    insert 
    into
        cst_customer
        (cust_name, cust_user_id, cust_create_id, cust_source, cust_industry, cust_level, cust_linkman, cust_phone, cust_mobile) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        cst_linkman
        (lkm_name, lkm_gender, lkm_phone, lkm_mobile, lkm_email, lkm_qq, lkm_position, lkm_memo, lkm_cust_id) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        cst_linkman
        (lkm_name, lkm_gender, lkm_phone, lkm_mobile, lkm_email, lkm_qq, lkm_position, lkm_memo, lkm_cust_id) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    update
        cst_linkman 
    set
        lkm_cust_id=? 
    where
        lkm_id=?
Hibernate: 
    update
        cst_linkman 
    set
        lkm_cust_id=? 
    where
        lkm_id=?

二、配置inverse="true"

2.1 配置


2.2 测试代码

 /**
  * cascade和inverse的区别
  */
 @Test
 public void run12() {
  Session session = HibernateUtils.getCurrentSession();
  Transaction tx = session.beginTransaction();

  // 级联保存
  Customer c1 = new Customer();
  c1.setCust_name("meimei");

  // 创建2个联系人
  Linkman l1 = new Linkman();
  l1.setLkm_name("xiongda");
  Linkman l2 = new Linkman();
  l2.setLkm_name("xionger");

  // 单向关联
  c1.getLinkmans().add(l1);
  c1.getLinkmans().add(l2);

  // 保存数据
  session.save(c1);

  tx.commit();
 }

2.3 运行效果


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: 
    insert 
    into
        cst_customer
        (cust_name, cust_user_id, cust_create_id, cust_source, cust_industry, cust_level, cust_linkman, cust_phone, cust_mobile) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        cst_linkman
        (lkm_name, lkm_gender, lkm_phone, lkm_mobile, lkm_email, lkm_qq, lkm_position, lkm_memo, lkm_cust_id) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        cst_linkman
        (lkm_name, lkm_gender, lkm_phone, lkm_mobile, lkm_email, lkm_qq, lkm_position, lkm_memo, lkm_cust_id) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?)


三、最终方案

在一方配置inverse="true",在多方配置cascade="save-update"

3.1 配置



3.2 测试代码

 /**
  * save的最终方案,开发中就这么用
  */
 @Test
 public void run13() {
  Session session = HibernateUtils.getCurrentSession();
  Transaction tx = session.beginTransaction();

  // 级联保存
  Customer c1 = new Customer();
  c1.setCust_name("meimei");

  // 创建2个联系人
  Linkman l1 = new Linkman();
  l1.setLkm_name("xiongda");
  Linkman l2 = new Linkman();
  l2.setLkm_name("xionger");

  // 单向关联
  l1.setCustomer(c1);
  l2.setCustomer(c1);

  // 保存数据
  session.save(l1);
  session.save(l2);

  tx.commit();
 }

3.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: 
    insert 
    into
        cst_customer
        (cust_name, cust_user_id, cust_create_id, cust_source, cust_industry, cust_level, cust_linkman, cust_phone, cust_mobile) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        cst_linkman
        (lkm_name, lkm_gender, lkm_phone, lkm_mobile, lkm_email, lkm_qq, lkm_position, lkm_memo, lkm_cust_id) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        cst_linkman
        (lkm_name, lkm_gender, lkm_phone, lkm_mobile, lkm_email, lkm_qq, lkm_position, lkm_memo, lkm_cust_id) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?)
源码下载

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP