Hibernate系列教材 (十六)- 各类概念

步骤1:先运行,看到效果,再学习
步骤2:模仿和排错
步骤3:什么是级联
步骤4:4种级联
步骤5:delete 级联
步骤6:save-update 级联
步骤7:all和none级联

步骤 1 : 先运行,看到效果,再学习

老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。

步骤 2 : 模仿和排错

在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程

步骤 3 : 什么是级联

什么是级联? 简单的说,没有配置级联的时候,删除分类,其对应的产品不会被删除。 但是如果配置了恰当的级联,那么删除分类的时候,其对应的产品都会被删除掉。

步骤 4 : 4种级联

包括上一步说的删除用得级联,级联有4种类型:
all:所有操作都执行级联操作;
none:所有操作都不执行级联操作;
delete:删除时执行级联操作; 
save-update:保存和更新时执行级联操作;
级联通常用在one-many和many-to-many上,几乎不用在many-one上。

步骤 5 : delete 级联

一对多代码的基础之上,修改TestHibernate类为

Category c = (Category) s.get(Category.class3);

s.delete(c);


并且为Category.hbm.xml 加上 

<set name="products" cascade="delete" lazy="false">


运行代码就会发现,删除分类的时候,会把分类下对应的产品都删除掉,否则只会把产品对应的cid设置为空

delete 级联

package com.how2java.test;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import com.how2java.pojo.Category;

public class TestHibernate {

    public static void main(String[] args) {

        SessionFactory sf = new Configuration().configure().buildSessionFactory();

        Session s = sf.openSession();

        s.beginTransaction();

        Category c = (Category) s.get(Category.class3);

        s.delete(c);

        s.getTransaction().commit();

        s.close();

        sf.close();

    }

}

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

  

<hibernate-mapping package="com.how2java.pojo">

    <class name="Category" table="category_">

        <id name="id" column="id">

            <generator class="native">

            </generator>

        </id>

        <property name="name" />

  

        <set name="products" cascade="delete" lazy="false">

            <key column="cid" not-null="false" />

            <one-to-many class="Product" />

        </set>

                  

    </class>

      

</hibernate-mapping>

步骤 6 : save-update 级联

一对多代码的基础之上,修改TestHibernate类为

Category c = (Category) s.get(Category.class5);

Product p1 = new Product();

p1.setName("product_501");

Product p2 = new Product();

p2.setName("product_502");

Product p3 = new Product();

p3.setName("product_503");


并且为Category.hbm.xml 加上 

<set name="products" cascade="save-update" lazy="false">


运行代码就会发现,这3个瞬时状态的产品对象虽然没有添加到数据库里,但是在事务提交的时候,会把他们3个持久化。 如果没有cascade="save-update",就会报错

package com.how2java.test;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import com.how2java.pojo.Category;

import com.how2java.pojo.Product;

public class TestHibernate {

    public static void main(String[] args) {

        SessionFactory sf = new Configuration().configure().buildSessionFactory();

        Session s = sf.openSession();

        s.beginTransaction();

        Category c = (Category) s.get(Category.class5);

         

        Product p1 = new Product();

        p1.setName("product_501");

        Product p2 = new Product();

        p2.setName("product_502");

        Product p3 = new Product();

        p3.setName("product_503");

        c.getProducts().add(p1);

        c.getProducts().add(p2);

        c.getProducts().add(p3);

        s.getTransaction().commit();

        s.close();

        sf.close();

    }

     

    public static void main4delete(String[] args) {

          SessionFactory sf = new Configuration().configure().buildSessionFactory();

            Session s = sf.openSession();

            s.beginTransaction();

            Category c = (Category) s.get(Category.class3);

            s.delete(c);

            s.getTransaction().commit();

            s.close();

            sf.close();

    }

}

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

  

<hibernate-mapping package="com.how2java.pojo">

    <class name="Category" table="category_">

        <id name="id" column="id">

            <generator class="native">

            </generator>

        </id>

        <property name="name" />

  

        <set name="products" cascade="save-update" lazy="false">

            <key column="cid" not-null="false" />

            <one-to-many class="Product" />

        </set>

                  

    </class>

      

</hibernate-mapping>

步骤 7 : all和none级联

all 就是 delete+save-update
none 就是没有,默认就是none

关注我,每天准时更新Java技术知识
更多Java全栈内容,点击了解: https://how2j.cn/k/hibernate/hibernate-cascade/1053.html

原文地址:https://www.cnblogs.com/Lanht/p/12789407.html