hibernate-cascade级联关系

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="entity.Project" table="PROJECT" schema="RENT">
        <id name="proid" type="java.lang.Integer">
            <column name="PROID" precision="6" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="proname" type="java.lang.String">
            <column name="PRONAME" length="100" not-null="true" />
        </property>
        <set name="employees" table="PROEMP" schema="RENT" cascade="save-update">
            <key>
                <column name="RPROID" precision="6" scale="0" not-null="true" />
            </key>
            <many-to-many entity-name="entity.Employee">
                <column name="REMPID" precision="6" scale="0" not-null="true" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>
package Test;

import static org.junit.Assert.*;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import entity.Employee;
import entity.Project;

import util.HibernateSessionFactory;

public class Test5 {

    //多对多
    @SuppressWarnings("unchecked")
    @Test
    public void test1() {
        //一定要在配置中加级联保存的配置cascade="save-update"
        Session session = HibernateSessionFactory.getSession();
        Transaction tx=session.beginTransaction();
        try {
            Employee employee1 = new Employee(4, "duhe");
            Employee employee2 = new Employee(5, "赵六");

            Project project1 = new Project(4, "4号项目");
            Project project2 = new Project(5, "5号项目");
            //创建关联关系
            project1.getEmployees().add(employee1);
            project1.getEmployees().add(employee2);
            
            employee1.getProjects().add(project1);
            employee2.getProjects().add(project1);

            project2.getEmployees().add(employee1);
            
            employee1.getProjects().add(project2);
            
            session.save(project1);
            session.save(project2);
            
            
            tx.commit();
            System.out.println("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
            System.out.println("新增失败");
        }finally{
            session.close();
        }
    }

}

 

package Test;


import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import entity.Dept;
import entity.Emp;

import util.HibernateSessionFactory;

public class Test33 {

    // 级联增加
    @Test
    public void test1() {
        Session session = HibernateSessionFactory.getSession();
        Transaction tx=session.beginTransaction();
        try {
            //创建部门
            Dept dept=new Dept();
            dept.setDeptno(50);
            dept.setDname("行政部");
            dept.setLoc("china");
            
            //创建员工
            Emp emp1=new Emp();
            emp1.setEmpno((short)8001);
            emp1.setEname("liujuan");
            emp1.setJob("吃饭");
            emp1.setSal(9000D);
            Emp emp2=new Emp();
            emp2.setEmpno((short)8002);
            emp2.setEname("duheyu");
            emp2.setJob("睡觉");
            emp2.setSal(1000D);
            
            //创建员工与部门关联关系
            Set<Emp> emps=new HashSet<Emp>();
            emps.add(emp1);
            emps.add(emp2);
            dept.setEmps(emps);
            emp1.setDept(dept);
            emp2.setDept(dept);
            
            //增加部门
            session.save(dept);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        }
        
    }

    // 级联删除
    @Test
    public void test2() {
        Session session = HibernateSessionFactory.getSession();
        Transaction tx=session.beginTransaction();
        try {
            /*Dept dept=new Dept();
            dept.setDeptno(50);*/
            
            //删除一个持久化对象
            Dept dept=(Dept) session.get(Dept.class, 50);
            session.delete(dept);
            tx.commit();
            System.out.println("删除成功");
        } catch (Exception e) {
            tx.rollback();
            System.out.println("删除失败");
        }
    }

    // 级联查询
    @Test
    public void test3() {
        Session session = HibernateSessionFactory.getSession();
        try {
            Dept dept = (Dept) session.get(Dept.class, 30);
            Set<Emp> emps= dept.getEmps();
            for (Emp emp : emps) {
                System.out.println(emp.getEname()+"\t"+emp.getDept().getDeptno()+"\t"+emp.getSal());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    //lazy属性:一对多。在一方配置
    @Test
    public void test4() {
        Session session = HibernateSessionFactory.getSession();
        try {
            //没有用到emp,默认延迟加载 Dept.hbm.xml set中默认是配置lasy="true",延迟加载
            Dept dept = (Dept) session.get(Dept.class, 30);
            Set<Emp> emps= dept.getEmps();
            //用到了才会去查询
            for (Emp emp : emps) {
                System.out.println(emp.getEname()+"\t"+emp.getDept().getDeptno()+"\t"+emp.getSal());
            }
            //加强延迟加载,使用于聚合函数查询中
            System.out.println(emps.size());//统计一共有几条,要有select count(*)语句
            //因为上面for循环查过一次,所以这里再统计,就不会再查一次。
            //如果不写for循环,只输出总数,那么也会是2条语句,不会查询所有信息,只输出聚合函数的查询语句
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

posted @ 2016-11-06 20:13  sincoolvip  阅读(314)  评论(0编辑  收藏  举报