hibernate 多对多 级联删除 注解

问题描述

有两个类: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集合类的引用,级联是建立在引用的基础上的。如果不想引用的话,手动删除就行了

时间: 2024-12-30 18:25:09

hibernate 多对多 级联删除 注解的相关文章

hibernate注解多对多级联保存怎么解

问题描述 hibernate注解多对多级联保存怎么解 @ManyToMany(mappedBy=""sysUsers""cascade={CascadeType.PERSISTCascadeType.MERGE}) private Set<SysDept> sysDept = new HashSet<SysDept>(0); //部门 @ManyToMany( cascade={CascadeType.PERSISTCascadeType.M

hibernate 级联删除问题

问题描述 小女子大问题====hibernate级联删除问题====现有相册(Album)和照片2个表(Photo)我想在删除相册的同时删除相册里所有的照片两个表的实体和映射如下publicclassAlbumimplementsSerializable{privateintid;privateStringname;//名称privateStringdescription;//描述privateStringpwd;//密码privateAlbumStatealbumState;//状态多对一单向

EF里一对一、一对多、多对多关系的配置和级联删除

原文:EF里一对一.一对多.多对多关系的配置和级联删除 本章节开始了解EF的各种关系.如果你对EF里实体间的各种关系还不是很熟悉,可以看看我的思路,能帮你更快的理解. I.实体间一对一的关系 添加一个PersonPhoto类,表示用户照片类 /// <summary> /// 用户照片类 /// </summary> public class PersonPhoto { [Key] public int PersonId { get; set; } public byte[] Ph

hibernate 级联删除

问题描述 struts+spring+hibernate级联删除 解决方案 解决方案二:这个是级联删除的<classname="Address">从表</class><classname="Person"casecade="all">主表</class>解决方案三:<classname="Xxxx"/><!--从表--><classname=&quo

hibernate级联删除时无法删除,一对多双向关联,一是Consult 多是TbFiles

问题描述 hibernate级联删除时无法删除,一对多双向关联,一是Consult 多是TbFiles 一的配置: 多的配置: class="com.project.dto.Consult" fetch="select" cascade="all"> 执行代码: public void delConsult(String[] ids){ HibernateTemplate hTemplate=this.getHibernateTemplat

hibernate级联删除操作失败

问题描述 对hibernate刚入手,不得其精华配置文件如下主表<class name="PrototypeOversea" table="T_MT_PROTOTYPEOVERSEA"><id name="sid" type="java.lang.Integer"><column name="SID" precision="10" scale="0

hibernate 多对多删除 中间表记录还在

问题描述 请问一下hibernate 多对多删除一方的记录的时候,为什么中间表的记录没有跟着删除呢,但是我在存入的时候,中间表记录就自动生成了啊 问题补充:UserGroup表private Set<Paper> papers = new HashSet<Paper>();@ManyToMany@JoinTable(name="tm_paper_usergroup",joinColumns={@JoinColumn(name="usergroupid&

Hibernate表的级联问题

问题描述 我用单向的多对一关系将两张表级联起来了.查询没问题,可就是删除"一"这边的时候出现报错,删除"多"的时候没错.错误内容:java.sql.SQLException: Cannot delete or update a parent row: a foreign key constraint fails (`remis_weson`.`remis_ele_estate`, CONSTRAINT `FK231ADE28182E2484` FOREIGN KEY

[NHibernate]一对多关系(级联删除,级联添加)

目录 写在前面 文档与系列文章 一对多关系 一个例子 级联删除 级联保存 总结 写在前面 在前面的文章中,我们只使用了一个Customer类进行举例,而在客户.订单.产品中它们的关系,咱们并没有涉及,比如一个客户可以有一个或者多个订单,在数据库中变现为"主外键关系",有时也喜欢称为"父子关系".那么就让我们一起学习,在nhibernate中,是如何处理这种关系的吧? 文档与系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFacto