问题描述
有两个类:Survey和Questions,问卷类和题目类,它们是多对多的关系,即:一张试卷可以有多个题目,一个题目可以属于多个试卷,我用两个一对多,数据库用中间表来实现,并有第三个类SurveyQuestions. surveuy类如下: //问卷实体:包括各种属性@Entity@Table(name="_survey")public class Survey {private int id;// 问卷IDprivate String name;// 问卷标题private String des;// 问卷描述或者备注private boolean state;// 状态:是否开放private SurveyType surveyType;// 问卷类型private int count;// 问卷数量限制private Date createTime;// 问卷创建时间private Date publishTime;// 问卷发布时间private Date deadline;// 问卷截止提交日期private String password;// 问卷访问密码private boolean grade;//是否是打分问卷@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDes() {return des;}public void setDes(String des) {this.des = des;}public boolean isState() {return state;}public void setState(boolean state) {this.state = state;}@ManyToOne@JoinColumn(name = "type_id")public SurveyType getSurveyType() {return surveyType;}public void setSurveyType(SurveyType surveyType) {this.surveyType = surveyType;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public Date getPublishTime() {return publishTime;}public void setPublishTime(Date publishTime) {this.publishTime = publishTime;}public Date getDeadline() {return deadline;}public void setDeadline(Date deadline) {this.deadline = deadline;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public boolean isGrade() {return grade;}public void setGrade(boolean grade) {this.grade = grade;}questions类 <span></span> @Entity@Table(name="_questions")public class Questions {private int id;// 问题主键IDprivate QuestionType questionType;// 问题类型private String head;// 问题题目private String body;// 问题主体private String answer;// 预留字段,如果是网上答卷,则是答案private String remarks;// 预留字段,备注private int number;// 选项个数@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getHead() {return head;}public void setHead(String head) {this.head = head;}public String getBody() {return body;}public void setBody(String body) {this.body = body;}public String getAnswer() {return answer;}public void setAnswer(String answer) {this.answer = answer;}public String getRemarks() {return remarks;}public void setRemarks(String remarks) {this.remarks = remarks;}@ManyToOne(fetch=FetchType.LAZY)@JoinColumn(name = "type_id")public QuestionType getQuestionType() {return questionType;}public void setQuestionType(QuestionType questionType) {this.questionType = questionType;}@Column(nullable = true)public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}}surveyQuestion类 package com.questionnaire.model;import java.util.Date;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.OneToMany;import javax.persistence.Table;@Entity@Table(name = "_surveyquestion")public class SurveyQuestion {private int id;// 问卷题库关联表ID,主键private Survey survey;// 对应问卷ID,外键private Questions questions;// 对应题目ID,外键private int number;// 题号,即该题目在本问卷的题号@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}this.userIp = userIp;public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}@ManyToOne(cascade = CascadeType.ALL)@JoinColumn(name = "survey_id")public Survey getSurvey() {return survey;}public void setSurvey(Survey survey) {this.survey = survey;}@ManyToOne(cascade = CascadeType.ALL)@JoinColumn(name = "questions_id")public Questions getQuestions() {return questions;}public void setQuestions(Questions questions) {this.questions = questions;}}我想实现删除题目时删除_questions表里的数据,同时删除_surveyQuestion表里的数据。当然survey同样?怎样实现呢?
解决方案
我也是初学者,好像要级联删除的话,必须Survey和Question类中要有SurveyQuestion集合类的引用,级联是建立在引用的基础上的。如果不想引用的话,手动删除就行了