package model;import java.util.Date;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;@Entity@Table(name="t_exam")public class Exam { private int id; private Student student; private Paper paper; private int singleScore; private int moreScore; private int score; private Date examDate; @Id @GeneratedValue(generator="_native") @GenericGenerator(name="_native",strategy="native") public int getId() { return id; } public void setId(int id) { this.id = id; } @ManyToOne @JoinColumn(name="studentId") public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } @ManyToOne @JoinColumn(name="paperId") public Paper getPaper() { return paper; } public void setPaper(Paper paper) { this.paper = paper; } public int getSingleScore() { return singleScore; } public void setSingleScore(int singleScore) { this.singleScore = singleScore; } public int getMoreScore() { return moreScore; } public void setMoreScore(int moreScore) { this.moreScore = moreScore; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public Date getExamDate() { return examDate; } public void setExamDate(Date examDate) { this.examDate = examDate; }}
package model;import java.util.ArrayList;import java.util.List;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.OneToMany;import javax.persistence.Table;import javax.persistence.Transient;import org.hibernate.annotations.Cascade;import org.hibernate.annotations.CascadeType;@Entity@Table(name="t_student")public class Student { private String id; private String name; private String password; private String sex; private String prefession; private String cardNo; private String flag="2"; private ListexamList = new ArrayList (); @Id @Column(name="id",unique=true,nullable=false,length=40) public String getId() { return id; } public void setId(String id) { this.id = id; } @Column(name="name",length=20) public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="password",length=20) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Column(name="sex",length=5) public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Column(name="prefession",length=40) public String getPrefession() { return prefession; } public void setPrefession(String prefession) { this.prefession = prefession; } @Column(name="cardNo",length=50) public String getCardNo() { return cardNo; } public void setCardNo(String cardNo) { this.cardNo = cardNo; } @Transient public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } //一对多的关系 ,级联删除 @OneToMany(mappedBy="student") @Cascade(CascadeType.DELETE) public List getExamList() { return examList; } public void setExamList(List examList) { this.examList = examList; }}
public void studentDelete(Student student){ Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); session.delete(student); session.getTransaction().commit();}
控制台输出:
这是第一种情况 学生表没有考试成绩的信息
Hibernate: select student_.id, student_.cardNo as cardNo2_4_, student_.name as name3_4_, student_.password as password4_4_, student_.prefession as prefessi5_4_, student_.sex as sex6_4_ from t_student student_ where student_.id=?首先查询了学生表
Hibernate: select examlist0_.studentId as studentI7_4_0_, examlist0_.id as id1_0_0_, examlist0_.id as id1_0_1_, examlist0_.examDate as examDate2_0_1_, examlist0_.moreScore as moreScor3_0_1_, examlist0_.paperId as paperId6_0_1_, examlist0_.score as score4_0_1_, examlist0_.singleScore as singleSc5_0_1_, examlist0_.studentId as studentI7_0_1_, paper1_.id as id1_2_2_, paper1_.joinDate as joinDate2_2_2_, paper1_.paperName as paperNam3_2_2_ from t_exam examlist0_ left outer join t_paper paper1_ on examlist0_.paperId=paper1_.id where examlist0_.studentId=?其次查询学生和考试成绩表 这是学生表下面没有人有成绩
Hibernate: delete from t_student where id=?
这是第二种情况 学生表里有考试成绩的信息
Hibernate: select student_.id, student_.cardNo as cardNo2_4_, student_.name as name3_4_, student_.password as password4_4_, student_.prefession as prefessi5_4_, student_.sex as sex6_4_ from t_student student_ where student_.id=?
Hibernate: select examlist0_.studentId as studentI7_4_0_, examlist0_.id as id1_0_0_, examlist0_.id as id1_0_1_, examlist0_.examDate as examDate2_0_1_, examlist0_.moreScore as moreScor3_0_1_, examlist0_.paperId as paperId6_0_1_, examlist0_.score as score4_0_1_, examlist0_.singleScore as singleSc5_0_1_, examlist0_.studentId as studentI7_0_1_, paper1_.id as id1_2_2_, paper1_.joinDate as joinDate2_2_2_, paper1_.paperName as paperNam3_2_2_ from t_exam examlist0_ left outer join t_paper paper1_ on examlist0_.paperId=paper1_.id where examlist0_.studentId=?
Hibernate: select questions0_.paperId as paperId10_2_0_, questions0_.id as id1_3_0_, questions0_.id as id1_3_1_, questions0_.answer as answer2_3_1_, questions0_.joinTime as joinTime3_3_1_, questions0_.optionA as optionA4_3_1_, questions0_.optionB as optionB5_3_1_, questions0_.optionC as optionC6_3_1_, questions0_.optionD as optionD7_3_1_, questions0_.paperId as paperId10_3_1_, questions0_.subject as subject8_3_1_, questions0_.type as type9_3_1_ from t_question questions0_ where questions0_.paperId=?
Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?Hibernate: delete from t_exam where id=?先删除成绩表里面的信息
Hibernate: delete from t_student where id=?再删除学生表的信息