码迷,mamicode.com
首页 > 其他好文 > 详细

一对多的单向操作

时间:2015-05-24 14:12:03      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

    /**
     *     Cascade:
            Save-update
                在session.save/update一个对象的时候,级联操作关联对象,关联对象或者执行save语句或者执行update语句或者什么都不执行
            Delete
                在session.delete一个对象的时候,级联删除关联对象
            All
                Save-update和delete的结合
     */
    
    /**
     * 保存班级
     */
    @Test
    public void test1(){
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Classes classes = new Classes();
        classes.setName("1班");
        classes.setDescription("111111111111班");
        session.save(classes);
        transaction.commit();
        session.close();
    }
    
    /**
     * 保存学生
     */
    @Test
    public void test2(){
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Student student = new Student();
        student.setName("学生1");
        student.setDescription("学生111111111111111");
        session.save(student);
        transaction.commit();
        session.close();
    }
    
    
    //级联操作
    /**
     * 保存班级的同时保存学生
     */
    @Test
    public void test3(){
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Student student = new Student();
        student.setName("111111");
        student.setDescription("班22222学生1");
        Student student2 = new Student();
        student2.setName("222222");
        student2.setDescription("班22222学生2");
        Classes classes = new Classes();
        classes.setName("班2");
        classes.setDescription("班22222222222");
        Set<Student> students = new HashSet<Student>();
        students.add(student);
        students.add(student2);
        classes.setStudents(students);
        session.save(classes);
        transaction.commit();
        session.close();
    }
    
    /**
     * 现有班级添加学生
     */
    @Test
    public void test4(){
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Classes classes = (Classes) session.get(Classes.class, 2L);
        Student student = new Student();
        student.setName("aaaaaaaa");
        student.setDescription("bbbbbbbbbbbbb");
        classes.getStudents().add(student);
        transaction.commit();
        session.close();
    }
    
    /**
     * 更改学生的班级
     */
    @Test
    public void test5(){
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Classes classes = (Classes) session.get(Classes.class, 1L);
        Student student = (Student) session.get(Student.class, 3L);
        classes.getStudents().add(student);
        transaction.commit();
        session.close();
    }
    
    /**
     * 解除关系
     */
    @Test
    public void test6(){
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Classes classes = (Classes) session.get(Classes.class, 1L);
        Student student = (Student) session.get(Student.class, 3L);
        classes.getStudents().remove(student);
        transaction.commit();
        session.close();
    }
    
    /**
     * 接触班级与该班所有学生的关系
     */
    @Test
    public void test7(){
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Classes classes = (Classes) session.get(Classes.class, 2L);
        classes.setStudents(null);
        
        
        transaction.commit();
        session.close();
    }
    
    /**
     * 删除班级
     * 删除班级级联删除学生
     */
    @Test
    public void test8(){
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Classes classes = (Classes) session.get(Classes.class, 1L);
        session.delete(classes);
        transaction.commit();
        session.close();
    }

 

一对多的单向操作

标签:

原文地址:http://www.cnblogs.com/jsnan/p/4525640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!