关闭

Hibernate 单向 1 对 多 对象关系映射 下 ,测试 crud

147人阅读 评论(0) 收藏 举报
分类:

Hibernate 单向 1 对 多 对象关系映射 下 


~~~~~~~~~~~~~~~~~~~~~~~~分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1. 增:save() 方法

@Test
	public void testSave() {
		
		Student student1 = new Student();
		student1.setStuName("ZhaoYu");
		
		Student student2 = new Student();
		student2.setStuName("LiuBe");
		
		
		Classbj classbj = new Classbj();
		classbj.setClassName("2016A");
		
		Set<Student> students = classbj.getStus();
		classbj.setStus(students);
		
		students.add(student1);
		students.add(student2);
		
		
		session.save(student1);
		session.save(student2);
		session.save(classbj);
	}
运行结果:n 条insert ,n-1条update
Hibernate: 
    insert 
    into
        STUDENTS
        (STU_NAME) 
    values
        (?)
Hibernate: 
    insert 
    into
        STUDENTS
        (STU_NAME) 
    values
        (?)
Hibernate: 
    insert 
    into
        CLASSBJS
        (CLASS_NAME) 
    values
        (?)
Hibernate: 
    update
        STUDENTS 
    set
        CLASS_IDQ=? 
    where
        STU_ID=?
Hibernate: 
    update
        STUDENTS 
    set
        CLASS_IDQ=? 
    where
        STU_ID=?

其他的 delete() 、update()、get() 方法 没有什么可说的!

	@Test
	public void testDelete() {
//		Classbj classbj = (Classbj) session.get(Classbj.class, 2);
//		 session.delete(classbj);
		 
		 Student student = (Student) session.get(Student.class, 5);
		 session.delete(student);
	}
	@Test
	public void testUpdate() {
//		Classbj classbj = (Classbj) session.get(Classbj.class, 3);
//		classbj.setClassName("2007A");
		
		Student student = (Student) session.get(Student.class, 6);
		student.setStuName("GuanYu");
	}

	
	
	@Test
	public void testGet() {
		Student student = (Student) session.get(Student.class, 7);
		System.out.println(student.getClass());
		
		Classbj classbj = (Classbj) session.get(Classbj.class, 4);
		System.out.println(classbj.getClassName());
		
		Set<Student> stus = classbj.getStus();
		
		for(Student stu:stus){
			System.out.println(stu);
		}
	}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:114204次
    • 积分:2523
    • 等级:
    • 排名:第15983名
    • 原创:135篇
    • 转载:47篇
    • 译文:0篇
    • 评论:17条
    最新评论