Remove method does not work, nor returns error

Asked

Viewed 179 times

1

My method remove does not delete the record on BD. And, worst, returns no error, nor executes the query (except for Select, I think it’s normal).

I used the prints to know if the object was arriving from Controller to the Repository, as well as if the find at the bank was successful and, yes, everything okay until then.

Where am I going wrong?

Alunosrepository.java

public void apagar(Long id) {
      try {
      Aluno aluno = new Aluno();
      System.out.print("Meu ID: " + id);
      // Busca a pessoa na base de dados através do seu ID.
      aluno = manager.find(Aluno.class, id);
      System.out.print(aluno.getNomeAluno());
      // Remove a pessoa da base de dados.
      manager.remove(aluno);
      } catch (Exception e) {

      }

Managementoalunobean.java

public void deletar() {
    alunosRepository.apagar(alunoSelecionado.getId());
    alunoSelecionado = null;

}

Java student.

@Entity
@Table(name = "aluno")
public class Aluno {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotEmpty
    private String nomeAluno;

    @NotEmpty
    @CPF
    private String cpf;

    @NotNull
    private Integer numMatricula;

    @NotEmpty
    private String telefone;

    @NotEmpty
    private String email;

    @NotEmpty
    private String curso;

    @Temporal(TemporalType.DATE)
    @Column(name = "data_nascimento")
    private Date dataNascimento;

    @OneToOne 
    private Endereco endereco;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNomeAluno() {
        return nomeAluno;
    }

    public void setNomeAluno(String nomeAluno) {
        this.nomeAluno = nomeAluno;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public Integer getNumMatricula() {
        return numMatricula;
    }

    public void setNumMatricula(Integer numMatricula) {
        this.numMatricula = numMatricula;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getDataNascimento() {
        return dataNascimento;
    }

    public void setDataNascimento(Date dataNascimento) {
        this.dataNascimento = dataNascimento;
    }

    public String getCurso() {
        return curso;
    }

    public void setCurso(String curso) {
        this.curso = curso;
    }

    public Endereco getEndereco() {
        return endereco;
    }

    public void setEndereco(Endereco endereco) {
        this.endereco = endereco;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Aluno other = (Aluno) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
}

Addressjava.

@Entity

@Table(name = "endereco") 
public class Endereco {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotEmpty
    private String logradouro;

    @NotEmpty
    private String bairro;

    @NotEmpty
    private String cep;

    @NotEmpty
    private String complemento;

    @NotEmpty
    private String cidade;

    @NotEmpty
    private String uf;

    @OneToOne (mappedBy = "endereco", cascade = CascadeType.ALL, orphanRemoval = true)
    private Aluno aluno;

    @OneToOne (mappedBy = "endereco", cascade = CascadeType.ALL, orphanRemoval = true)
    private Professor professor;

    @OneToOne (mappedBy = "endereco", cascade = CascadeType.ALL, orphanRemoval = true)
    private Coordenador coordenador;

    public Professor getProfessor() {
        return professor;
    }

    public void setProfessor(Professor professor) {
        this.professor = professor;
    }

    public Coordenador getCoordenador() {
        return coordenador;
    }

    public void setCoordenador(Coordenador coordenador) {
        this.coordenador = coordenador;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLogradouro() {
        return logradouro;
    }

    public void setLogradouro(String logradouro) {
        this.logradouro = logradouro;
    }

    public String getBairro() {
        return bairro;
    }

    public void setBairro(String bairro) {
        this.bairro = bairro;
    }

    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }

    public String getComplemento() {
        return complemento;
    }

    public void setComplemento(String complemento) {
        this.complemento = complemento;
    }

    public String getCidade() {
        return cidade;
    }

    public void setCidade(String cidade) {
        this.cidade = cidade;
    }

    public String getUf() {
        return uf;
    }

    public void setUf(String uf) {
        this.uf = uf;
    }

    public Aluno getAluno() {
        return aluno;
    }

    public void setAluno(Aluno aluno) {
        this.aluno = aluno;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Endereco other = (Endereco) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
}
  • 5

    You have an empty Try-catch on AlunosRepository. How do you make sure you’re not bursting any bugs?

  • 1

    In fact, it does return an error, but you’re not doing anything with it, and as Pedro said, your catch is empty, put a system.out.println(e); and update your question with the error returned.

  • First of all, thank you, gentlemen. I did exactly as @Luiseduardo did, yet nothing was featured on the console. Would it be a relationship problem? Something to do with Onetoone? I’m editing to add the Address layer.

  • Yes, it has to do, if you are trying to delete content from a parent table, and this content has children and you have not defined Cache in your parent model, in addition to not removing the content you want, it returns an error, which in your case I believe is just that.

1 answer

0

I went through the same problem, what I did was basically return the object by find, then merge and commit into this.

private final static EntityManager manager = ConnectionFactory.getEntityManager();

public T findById2(Class<T> clazz, Long id) {
    manager.getTransaction().begin();
    return manager.find(clazz, id);
}

public void remove(T t) {
    manager.merge(t);
    manager.remove(t);
    manager.getTransaction().commit();
}

Browser other questions tagged

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