关于Hibernate的关联映射(annotation专版)
举一典型到老掉牙的例子说明:Teacher、Student与Course之间的关系。
Teacher与Course是一对一关系;
Student与Course是一对多关系;
Teacher与Student是多对多关系;
....................
@Entity
@Table(name="Student")
public class Student {
private int id;
private String name;
private String sex;
private Course course;
private Set<Teacher> teachers = new HashSet<Teacher>();
//建立Student与Teacher的双向多对多关联
@ManyToMany(mappedBy="students")
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
//单向多对一关联
@ManyToOne
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
@Id
@Column(name="s_id")
public int getId() {
return id;
}
@Column(name="s_name")
public String getName() {
return name;
}
@Column(name="s_sex")
public String getSex() {
return sex;
}
.............
}
..............................................
@Entity
@Table(name="Teacher")
public class Teacher {
private int id;
private String name;
private String sex;
private String title;
private Course course;
private Set<Student> students = new HashSet<Student>();
//Teacher与Student的单向多对多联系
@ManyToMany
@JoinTable(name="t_s",
joinColumns={@JoinColumn(name="t_id")},
inverseJoinColumns={@JoinColumn(name="s_id")}
)
//设置多对多关系的中间表,其中joinColumns属性里面指向本实体数据表的关联字段,
//inverseJoinColumns属性则指向中间表的另一关联字段对应的数据表的关联字段
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
//Teacher与Course的一对一关联
@OneToOne
@JoinColumn(name="Course_id")
//JoinColumn,指定Course表的外键
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
@Id
@Column(name="t_id")
public int getId() {
return id;
}
@Column(name="t_name")
public String getName() {
return name;
}
@Column(name="t_sex")
public String getSex() {
return sex;
}
@Column(name="t_title")
public String getTitle() {
return title;
}
..................
}
..........................
@Entity
@Table(name="Course")
public class Course {
private int id;
private String name;
private String score;
private Set<Student> student = new HashSet<Student>();
private Teacher teacher;
/*
* 为实现双向的一对一关联,添加属性mappedBy,使确定该实体被映射在teacher中的course属性上,主导放在多的一方
* */
@OneToOne(mappedBy="course")
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
/*
* 为实现双向的多对一关联,添加属性mappedBy,使确定该实体被映射在student中的course属性上,主导放在多的一方
* */
@OneToMany(mappedBy="course")
//单向一对多关联
//@JoinColumn(name="Course_id")
public Set<Student> getStudent() {
return student;
}
public void setStudent(Set<Student> student) {
this.student = student;
}
@Id
@Column(name="c_id")
public int getId() {
return id;
}
@Column(name="c_name")
public String getName() {
return name;
}
@Column(name="c_score")
public String getScore() {
return score;
}
...................
}
以下是Junit测试的一个方法,用来查看Hibernate生成SQl脚本的
@Test
public void testSchemaExport(){
new SchemaExport(
new AnnotationConfiguration().configure())
.create(true, true);
}