木叶下521
2013-11-27 18:21
浏览 405
已采纳

hibernate 多对多 级联删除 注解

有两个类:Survey和Questions,问卷类和题目类,它们是多对多的关系,即:一张试卷可以有多个题目,一个题目可以属于多个试卷,我用两个一对多,数据库用中间表来实现,并有第三个类SurveyQuestions.
surveuy类如下: [code="java"]//问卷实体:包括各种属性
@Entity
@Table(name="_survey")
public class Survey {
private int id;// 问卷ID
private 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
@GeneratedValue
public 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;
}[/code]

questions类 [code="java"] @Entity
@Table(name="_questions")
public class Questions {
private int id;// 问题主键ID
private QuestionType questionType;// 问题类型
private String head;// 问题题目
private String body;// 问题主体
private String answer;// 预留字段,如果是网上答卷,则是答案
private String remarks;// 预留字段,备注
private int number;// 选项个数

@Id
@GeneratedValue
public 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;
}

}[/code]
surveyQuestion类 [code="java"]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
@GeneratedValue
public 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;
}

}[/code]
我想实现删除题目时删除_questions表里的数据,同时删除_surveyQuestion表里的数据。当然survey同样?怎样实现呢?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • sh_peterchen 2013-11-30 18:25
    已采纳

    我也是初学者,好像要级联删除的话,必须Survey和Question类中要有SurveyQuestion集合类的引用,级联是建立在引用的基础上的。如果不想引用的话,手动删除就行了

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题