package com.gem.pojo;
//stu 类
public class Stu {
private int stuId;
private String stuentName;
private ClassTbl tbl;
public ClassTbl getClassTbl() {
return tbl;
}
public void setClassTbl(ClassTbl tbl) {
this.tbl = tbl;
}
public String getStuentName() {
return stuentName;
}
public void setStuentName(String stuentName) {
this.stuentName = stuentName;
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
}
/************************************************************/
package com.gem.pojo;
import java.util.Set;
public class ClassTbl {
private int classId;
private String className;
private Set stu;
public Set getStu() {
return stu;
}
public void setStu(Set stu) {
this.stu = stu;
}
public int getClassId() {
return classId;
}
public void setClassId(int classId) {
this.classId = classId;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
//classTbl与stu 之间的关系是一对多关系
/*************************************************************/
//测试类(main)
package com.gem.test;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.gem.dao.HibernateSessionFactory;
import com.gem.pojo.ClassTbl;
import com.gem.pojo.Stu;
public class MethodTest {
@SuppressWarnings("unchecked")
public static void main(String[] args){
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
//获得该学生ID
Stu stu = (Stu) session.get(Stu.class, 2);
//移除学生与班级的关系(类)
ClassTbl tb = (ClassTbl) session.get(ClassTbl.class, 3);
//stu.getClassTbl().setStu(null);
//删除该学生
session.delete(stu);
tx.commit();
session.close();
}
}