这是列表类
@Entity
@Table(name = "list")
public class List extends AbstractEntity {
@NotNull
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "date")
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private Date date;
//getters and setters
}
以下是执行类
@Entity
@Table(name = "EXECUTION")
public class Execution extends AbstractEntity {
@ManyToOne(targetEntity = List.class)
@JoinColumn(name = "list_id")
private List list;
@Column(name = "date")
@NotNull
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private Date date;
}
以下是删除列表的控制器方法
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteList(@PathVariable("id") Long listId) {
List list = listRepository.findOne(listId);
listRepository.delete(listId);
}
当我执行上面的代码时,我收到以下错误
: ERROR: update or delete on table "list" violates foreign key constraint "fkmnja1nmiq9v42f0ojydccxf20" on table "execution"
细节:Key(id)=(1)仍然从表&#34执行&#34 ;.
在删除具有特定ID的列表表之前,如何删除带有列表ID的执行表?
任何帮助将不胜感激。谢谢你
答案 0 :(得分:1)
定义关系中的级联行为,
@Entity
@Table(name = "EXECUTION")
public class Execution extends AbstractEntity {
//add cascade , so on delete of list all the execution will also be removed
@ManyToOne(targetEntity = List.class, cascade = {CascadeType.ALL})
@JoinColumn(name = "list_id")
private List list;
@Column(name = "date")
@NotNull
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private Date date;
}