奔跑在路上

奔跑在路上的烂笔头!

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

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);
		}
	}



阅读更多
版权声明:本文为博主原创文章,如果对你有用,敬请带走! https://blog.csdn.net/chuck_kui/article/details/51548225
个人分类: Hibernate
上一篇Hibernate 单向 多 对 1 对象关系映射 下 ,测试 crud
下一篇Hibernate 双向 多 对 1 对象关系映射 下 ,测试 crud
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭