I have a system on which to register Instructors and Students, each Instructor may have several Students under supervision and each Student may have only one Instructor or no Instructor.
My problem is that when deleting an Instructor, students related to it are also deleted, as well as when deleting the student, your instructor is deleted. This happens when I use the CascadeType.All
annotation, when I do not use annotation, it does not allow me to delete anything under any hypothesis. What would be the correct way to deal with this situation?
I want to delete an Instructor without deleting their Students, leaving the field that links them to null. Thus, you can also delete a Student without affecting the Instructor.
Student Class:
@Entity
public class Aluno {
@Id
@GeneratedValue
@Column(name = "id_aluno", nullable = false)
private Integer id;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "id_instrutor", nullable = true)
private Instrutor instrutor;
@Column(name = "nm_aluno", nullable = false)
private String nome;
@Column(name = "sexo", nullable = false)
private char sexo;
@Column(name = "data_nasc", nullable = true)
@Temporal(TemporalType.DATE)
private Date DataNascimento;
@Column(name = "email_aluno", nullable = false)
private String email;
@Column(name = "login_aluno", nullable = false)
private String login;
@Column(name = "senha_aluno", nullable = false)
private String senha;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Instrutor getInstrutor() {
return instrutor;
}
public void setInstrutor(Instrutor instrutor) {
this.instrutor = instrutor;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public char getSexo() {
return sexo;
}
public void setSexo(char sexo) {
this.sexo = sexo;
}
public Date getDataNascimento() {
return DataNascimento;
}
public void setDataNascimento(Date dataNascimento) {
DataNascimento = dataNascimento;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
}
Instructor Class
@Entity
public class Instrutor implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "id_instrutor", nullable = false)
private Integer id;
@Column(name = "nm_instrutor", nullable = false)
private String nome;
@Column(name = "desc_instrutor", nullable = false)
private String descricao;
@Column(name = "email_instrutor", nullable = false)
private String email;
@Column(name = "login_instrutor", nullable = false)
private String login;
@Column(name = "senha_instrutor", nullable = false)
private String senha;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "instrutor")
private List<Aluno> alunos;
public Instrutor() {
}
public Instrutor(String nome, String descricao, String email, String login,
String senha) {
this.nome = nome;
this.descricao = descricao;
this.email = email;
this.login = login;
this.senha = senha;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public List<Aluno> getAlunos() {
return alunos;
}
public void setAlunos(List<Aluno> alunos) {
this.alunos = alunos;
}
}