Delete From Database without deleting related objects

Asked

Viewed 75 times

1

I have a system in which I register Instructors and Students, each Instructor can have several Students under supervision and each Student can have only one Instructor or no Instructor.

My problem is that when it comes to deleting an Instructor, students related to it are also erased, as well as when deleting the student, their instructor is erased. This occurs when I use the annotation CascadeType.All, when I don’t use annotation, it doesn’t allow me to delete anything under any circumstances. What would be the right way to deal with this situation?

I want to erase an Instructor, without his Students being erased, leaving null the field that relates them. Thus, also being able to erase a Student without affecting the Instructor.

Pupil 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;
    }

}
  • I wonder why people post pictures of codes and errors instead of copying and pasting their own texts.

  • 1

    I keep asking myself why some people care about making anything constructive criticism, when there’s no need for any of that.

  • I don’t mean to offend you, but it’s pretty clear that TEXT is much better than PHOTOS and makes it easier for people to answer, and it also makes it easier for search engines to find your question, unless in the future everything works with OCR. In my opinion it is a constructive criticism, since it will help other people to help you.

  • 1

    Since there was no intention of offending it could be straightforward to explain such arguments, rather than to comment sarcastically. Anyway, I’ll correct the question.

  • I WAS NOT SARCASTIC in no time friend. I’ll ask you a simple question. Do you expect people to test your code they have to rewrite everything in the photo? Isn’t it easier to post the code? - This goes for console errors as well. Another thing, in the future users with the same problem may not find their question that can help them!

1 answer

0

Try this, leave it as Cascade. but adds the attribute orphanRemoval = false in the annotation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.