Student类:
import java.util.HashSet;
import java.util.Set;
import javax.annotation.Generated;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "t_student")
@SequenceGenerator(name = "seq", sequenceName = "seq_teacher_id")
public class Student {
@Id
@GeneratedValue(generator = "seq")
Integer studentId;
String studentName;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "t_teacher_student", inverseJoinColumns = { @JoinColumn(name = "teacherid") }, joinColumns = { @JoinColumn(name = "studentid") })
Teacher teacher = new Teacher();
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
Teacher类:
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "t_teacher")
@SequenceGenerator(name = "seq", sequenceName = "seq_student_id")
public class Teacher {
@Id
@GeneratedValue(generator = "seq")
Integer teacherId;
String teacherName;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "teacher")
Set<Student> students = new HashSet<Student>(0);
public Integer getTeacherId() {
return teacherId;
}
public void setTeacherId(Integer teacherId) {
this.teacherId = teacherId;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
public void addStudent(Student student) {
this.students.add(student);
}
}
测试:
import org.hibernate.Session;
import org.hibernate.Transaction;
import commons.utils.HibernateUtil;
public class Test {
public static void main(String[] args) {
Session session = HibernateUtil.getCurrentSession();
session.getTransaction().begin();
Student s1, s2;
s1 = new Student();
s1.setStudentName("studentname1");
s2 = new Student();
s2.setStudentName("studentname2");
Teacher t1, t2;
t1 = new Teacher();
t1.setTeacherName("teacherName1");
t2 = new Teacher();
t2.setTeacherName("teacherName2");
s1.setTeacher(t1);
s2.setTeacher(t1);
session.save(s1);
session.save(s2);
session.save(t1);
session.save(t2);
session.getTransaction().commit();
session.flush();
session.close();
}
}