1.学生
@Entity
@Table(name = "STUDENT", schema = "SCOTT")
public class Student implements java.io.Serializable {
// Fields
private Integer sid;
private String sname;
private Set<Course> courses = new HashSet<Course>(0);
// Constructors
/** default constructor */
public Student() {
}
/** full constructor */
public Student(String sname, Set<Course> courses) {
this.sname = sname;
this.courses = courses;
}
// Property accessors
@Id
@GeneratedValue
@Column(name = "SID", unique = true, nullable = false, precision = 6, scale = 0)
public Integer getSid() {
return this.sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
@Column(name = "SNAME")
public String getSname() {
return this.sname;
}
public void setSname(String sname) {
this.sname = sname;
}
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "COURSE_STUDENT", schema = "SCOTT", joinColumns = {
@JoinColumn(name = "STUDENTSET_SID", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "COURSE_CID", nullable = false, updatable = false) })
public Set<Course> getCourses() {
return this.courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}
2.课程
@Entity
@Table(name = "COURSE", schema = "SCOTT")
public class Course implements java.io.Serializable {
// Fields
private Integer cid;
private String cname;
private Set<Student> students = new HashSet<Student>(0);
// Constructors
/** default constructor */
public Course() {
}
/** full constructor */
public Course(String cname, Set<Student> students) {
this.cname = cname;
this.students = students;
}
// Property accessors
@Id
@GeneratedValue
@Column(name = "CID", unique = true, nullable = false, precision = 6, scale = 0)
public Integer getCid() {
return this.cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
@Column(name = "CNAME")
public String getCname() {
return this.cname;
}
public void setCname(String cname) {
this.cname = cname;
}
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "courses")
public Set<Student> getStudents() {
return this.students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
3.测试 增,删,改
@Test
//增加
public void duotoduo(){
Session session=HibernateSessionFactory.getSession();
Transaction tx=null;
try {
tx=session.beginTransaction();
Course cur=new Course();//操控方
Course cur2=new Course();
cur.setCname("JS");
cur2.setCname("C#");
Student st=new Student();//被控方
Student st3=new Student();
Student st2=new Student();
st.setSname("逗比丁一");
st2.setSname("憨子丁一");
st3.setSname("傻子丁一");
st.getCourses().add(cur);
st.getCourses().add(cur2);
st2.getCourses().add(cur);
st3.getCourses().add(cur2);
session.save(st);
session.save(st2);//因为Mapping中有cascade="all" 所以不会有 违反外键约数
session.save(st3);
tx.commit();
} catch (Exception e) {
if(null!=tx)tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
}
//修改
@Test
public void duotoduoUpdate(){
Session session=HibernateSessionFactory.getSession();
Transaction tx=null;
try {
tx=session.beginTransaction();
Course c=(Course) session.load(Course.class, 245);
c.setCname("新JS");
session.update(c);
tx.commit();
} catch (Exception e) {
if(null!=tx)tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
}
//删除
@Test
public void duotoduoDelete(){
Session session=HibernateSessionFactory.getSession();
Transaction tx=null;
try {
tx=session.beginTransaction();
//Course c=(Course) session.load(Course.class, 277);
Student s=(Student) session.load(Student.class, 241);
session.delete(s);
tx.commit();
} catch (Exception e) {
if(null!=tx)tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
}