四组对象和级联

one-to-many

many-to-one

many-to-many

one-to-one

的四组对象关系,并使用hibernate实现写出

对应的domainobject和对象关系映射文件,并通过hbm2ddl 自动创建成功对应的表

 

级联操作

所谓级联操作就是说,当你进行某个操作(添加/修改/删除...),就由hibernate自动给你完成.

比如: Department <---->Student 对象关系,我希望当我删除一个department ,那么就自动删除该部门的所有学生?

 

再比如: bbs项目

主帖<---->回帖 , 把主帖删除,那我们就希望把该主帖的回帖自动删除,这样我们可以使用级联(cascade)操作

\

案例:如何配置级联操作,当删除某个部门的时候,我们自动删除其学生.

首先我们在 配置文件中修改:

 

java代码中操作:

//演示删除级联

                     //获取到某个部分

                     Departmentdepartment=(Department) s.get(Department.class, 41);

                     s.delete(department);

 

u  演示save-update

配置文件:

代码:

//添加学生

Department department=new Department();

department.setName("业务部门3");

                    

Student stu1=new Student();

stu1.setName("顺平6");

//     stu1.setDept(department);

                     Studentstu2=new Student();

                     stu2.setName("小明6");

              //     stu2.setDept(department);

 

                     Setstudents=new HashSet();

                     students.add(stu1);

                     students.add(stu2);

                     department.setStus(students);

                    

                     s.save(department);

说明:

在集合属性和普通属性中都能使用cascade

一般讲cascade配置在one-to-many(one的一方,比如Employee-Department),one-to-one(主对象一方)




多对一 对象

student.hbm.xml





 

stu_seq









department.hbm.xml










dept_seq







package com.hsp.domain;

public class Student implements java.io.Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private Department dept;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Department getDept() {
		return dept;
	}
	public void setDept(Department dept) {
		this.dept = dept;
	}
}

package com.hsp.domain;

public class Department implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}


四组对象和级联_第1张图片



1对1 (主键)


person.hbm.xml
















idcard.hbm.xml




person


四组对象和级联_第2张图片


1对1 (外键)

idcard.hbm.xml
















person.hbm.xml和上面一个例子一样


四组对象和级联_第3张图片

many to many

package com.hsp.domain;

import java.util.Set;

public class Course {
	private Integer id;
	private String name;
	private Set stuCourses;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set getStuCourses() {
		return stuCourses;
	}
	public void setStuCourses(Set stuCourses) {
		this.stuCourses = stuCourses;
	}
}



package com.hsp.domain;

public class StuCourse {
	private Integer id;
	private Student student;
	private Course course;
	private Integer grade;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public Course getCourse() {
		return course;
	}
	public void setCourse(Course course) {
		this.course = course;
	}
	public Integer getGrade() {
		return grade;
	}
	public void setGrade(Integer grade) {
		this.grade = grade;
	}
}




package com.hsp.domain;

import java.util.Set;

public class Student {
	private Integer id;
	private String name;
	private Set stuCourses;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set getStuCourses() {
		return stuCourses;
	}
	public void setStuCourses(Set stuCourses) {
		this.stuCourses = stuCourses;
	}
}

course.hbm.xml
!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">




course_seq












StuCourse.hbm.xml






stucourse_seq









Student.hbm.xml







stu_seq












四组对象和级联_第4张图片

级联









dept_seq


















 

stu_seq










你可能感兴趣的:(servlet)