zoukankan      html  css  js  c++  java
  • Hibernate课程 初探一对多映射2-7 测试-修改和删除学生信息

    package com.ddwei.entity;
    
    import java.util.Set;
    
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    
    import com.ddwei.util.HibernateUtil;
    
    /**
     * 建立关联关系后,
     * 可以方便的从一个导航对象,关联到另一个导航对象!
     * 注意关联方向:一对多
     * @author weijingli
     *
     */
    public class Test {
        
        public static void main(String[] args) {
    //        add();
    //        show();
    //        updateGrade();
            deleteStu();
        }
        
        
        //增加班级学生信息
        public static void add(){
            
            Grade g = new Grade("java一班","java一班是一个好班");
            Student stu1 = new Student("秦皇","男");
            Student stu2 = new Student("汉武","男");
            Student stu3 = new Student("唐宗","男");
            g.getStudents().add(stu1);
            g.getStudents().add(stu2);
            g.getStudents().add(stu3);
            Session session = HibernateUtil.getSession();
            Transaction tx = session.beginTransaction();
            session.save(g);
            session.save(stu1);
            session.save(stu2);
            session.save(stu3);
            tx.commit();
            HibernateUtil.closeSession(session);
        }
        
        
        //查询班级学生信息
        public static void show(){
            
            Session session1 = HibernateUtil.getSession();
            Grade grade1 = (Grade) session1.get(Grade.class, 1);//得到java1班
            System.out.println("班级名称为:"+grade1.getGname());
            Set<Student> students = grade1.getStudents();
            for(Student stu:students){
                System.out.println("学生姓名为:"+stu.getSname());
            }
            HibernateUtil.closeSession(session1);
        }
        
        
        //修改学生所在班级信息
        public static void updateGrade(){
            
            Grade grade2 = new Grade("java二班","java二班介绍");
            Session session2 = HibernateUtil.getSession();
            Transaction tx2 = session2.beginTransaction();
            Student student1 = (Student) session2.get(Student.class, 1);
            grade2.getStudents().add(student1);//java二班添加学号为1的学生
            session2.save(grade2);
            tx2.commit();
            HibernateUtil.closeSession(session2);
        } 
        
        //删除学生信息
        public static void deleteStu(){
            Session session2 = HibernateUtil.getSession();
            Transaction tx2 = session2.beginTransaction();
            Student stu2 = (Student)session2.get(Student.class, 3);
            session2.delete(stu2);
            tx2.commit();
            HibernateUtil.closeSession(session2);
        }
    
    }
  • 相关阅读:
    jQuery插件开发之定宽输出
    全国所有省市县地理坐标Json格式
    diff/merge configuration in TFS
    How to accelerate your VPC 2007
    TechNet Magazine/TechNet杂志
    net helpmsg get error message for error id in cmd script
    devenv.exe /DebugExe debug unitcase
    ZoomIt for demo
    diskpart to deal with driver letter changed after reboot in Virtual Machine
    几个关于web开发资源的站点
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/9363829.html
Copyright © 2011-2022 走看看