我正在完成基本任务以删除级联任务,但我得到了例外。
班级Missions
与班级OneToMany
和ResponsesCheckLists
与班级OneToMany
有StatistisquesMissions
关系。
我有以下实体:
Missions.java
@Entity
public class Missions implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="idMission")
private long idMission;
@Column(name="nomMission")
private String nomMission;
@OneToMany(mappedBy="Respmission", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Collection <ResponsesCheckLists> ResponsesCheckLists;
//getters and setters
@JsonIgnore
public Collection<ResponsesCheckLists> getResponsesCheckLists() {
return ResponsesCheckLists;
}
public void setResponsesCheckLists(Collection<ResponsesCheckLists> responsesCheckLists) {
ResponsesCheckLists = responsesCheckLists;
}
}
ResponsesCheckLists.java
@Entity
public class ResponsesCheckLists implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="idResponsesCHeck")
private long idResponsesCHeck;
@ManyToOne
@JoinColumn(name="missionsId")
private Missions Respmission;
@ManyToOne
@JoinColumn(name="checkLists_Id")
private CheckLists CheckLts;
//getters and setters
public Missions getRespmission() {
return Respmission;
}
public void setRespmission(Missions respmission) {
Respmission = respmission;
}
public CheckLists getCheckLts() {
return CheckLts;
}
public void setCheckLts(CheckLists checkLts) {
CheckLts = checkLts;
}
}
StatistisquesMessions.java
@Entity
public class StatistisquesMessions implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="idStat")
private long idStat;
//@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@ManyToOne
@JoinColumn(name="equipement_ids")
@JsonBackReference
private Equipements statEquipements;
@ManyToOne
@JoinColumn(name="statMission_id")
@JsonBackReference
private Missions statMissions;
}
当我想从MySQL数据库中删除Missions
时,如下所示(API REST):
@Override
public Boolean DeleteT(Pk id) {
T tt= GR.findOne(id);
GR.delete(tt);
return true;
}
@Autowired
private MissionsMetier missionM;
@RequestMapping(value="/DeleteMissions")
public Missions DeleteMissions(@Param(value ="idMission") long idMission){
Missions mis = missionM.getT(idMission); //I have the object "Missions"
return missionM.DeleteT(idMission);
}
然后我收到以下错误
无法删除或更新父行:外键约束失败 (
BDTasks
。responses_check_lists
,CONSTRAINTFK73o6po4goa6ulera1d6md58i6
FOREIGN KEY(missions_id
)参考missions
(id_mission
))
hibernate查询:
Hibernate: select stat0_.stat_mission_id as stat_mis8_9_0_, stat0_.id_stat as id_stat1_9_0_, stat0_.id_stat as id_stat1_9_1_, stat0_.nbre_conforme as nbre_con2_9_1_, stat0_.nbre_not_conforme as nbre_not3_9_1_, stat0_.res_conforme as res_conf4_9_1_, stat0_.res_not_conforme as res_not_5_9_1_, stat0_.equipement_ids as equipeme7_9_1_, stat0_.stat_mission_id as stat_mis8_9_1_, stat0_.total_check_lists as total_ch6_9_1_, equipement1_.id_equipements as id_equip1_3_2_, equipement1_.actifs_id as actifs_i5_3_2_, equipement1_.date_ajout_eq as date_ajo2_3_2_, equipement1_.date_modification_eq as date_mod3_3_2_, equipement1_.nom_eq as nom_eq4_3_2_, actifs2_.id_actif as id_actif1_0_3_, actifs2_.date_ajout_actif as date_ajo2_0_3_, actifs2_.nom_actif as nom_acti3_0_3_ from statistisques_messions stat0_ left outer join equipements equipement1_ on stat0_.equipement_ids=equipement1_.id_equipements left outer join actifs actifs2_ on equipement1_.actifs_id=actifs2_.id_actif where stat0_.stat_mission_id=?
Hibernate: delete from statistisques_messions where id_stat=?
Hibernate: delete from missions where id_mission=?
EDIT1:
@Autowired
private MissionsMetier missionM;
@RequestMapping(value="/DeleteMissions")
public Missions DeleteMissions(@Param(value ="idMission") long idMission){
Missions mis = missionM.getT(idMission); //I have the object "Missions"
// delete ResponsesCheckLists
Collection<ResponsesCheckLists> Rp = res.FinsRespoCHeckListsMission(idMission);
for(ResponsesCheckLists r : Rp){
System.out.println("***** id response "+r.getIdResponsesCHeck());
res.DeleteT(r.getIdResponsesCHeck());
}
//delete StatistisquesMessions
Collection<StatistisquesMessions> st =mis.getStat();
for(StatistisquesMessions stat : st){
statis.DeleteT(stat.getIdStat());
}
mis.removeMissionsFromUsers();
// return missionM.DeleteT(idMission);
//return true;
return missionM.DeleteT(idMission);
return missionM.DeleteT(idMission);
}
我遇到同样的问题
EDIT2:
@Autowired
private MissionsMetier missionM;
@RequestMapping(value="/DeleteMissions")
public Missions DeleteMissions(@Param(value ="idMission") long idMission){
Missions mis = missionM.getT(idMission); //I have the object "Missions"
// delete ResponsesCheckLists
Collection<ResponsesCheckLists> Rp = res.FinsRespoCHeckListsMission(idMission);
for(ResponsesCheckLists r : Rp){
System.out.println("***** id response "+r.getIdResponsesCHeck());
res.DeleteT(r.getIdResponsesCHeck());
}
//delete StatistisquesMessions
Collection<StatistisquesMessions> st =mis.getStat();
for(StatistisquesMessions stat : st){
statis.DeleteT(stat.getIdStat());
}
mis.removeMissionsFromUsers();
return missionM.DeleteT(mis.getIdMission());
}
如何删除其Missions
及其ResponsesCheckLists
个孩子的StatistisquesMessions
个?
非常感谢,
答案 0 :(得分:0)
通过以下代码解决了该问题:
@RequestMapping(value="/DeleteMissions")
public Missions DeleteMissions(@Param(value ="idMission") long idMission){
Missions mis = missionM.getT(idMission); //I have the object "Missions"
// delete ResponsesCheckLists
Collection<ResponsesCheckLists> Rp = res.FinsRespoCHeckListsMission(idMission);
for(ResponsesCheckLists r : Rp){
r.getCheckLts().setResponsesChecks(null);
r.setCheckLts(null);
res.saveT(r);
res.DeleteT(r.getIdResponsesCHeck());
}
//delete StatistisquesMessions
Collection<StatistisquesMessions> st =mis.getStat();
for(StatistisquesMessions stat : st){
statis.DeleteT(stat.getIdStat());
}
return missionM.DeleteT(idMission);
}