Last active
January 2, 2018 22:25
Star
You must be signed in to star a gist
Por quê não consigo apagar um objeto com o Hibernate?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import java.io.Serializable; | |
import java.lang.reflect.ParameterizedType; | |
import java.util.List; | |
import javax.inject.Inject; | |
import javax.persistence.EntityManager; | |
import javax.persistence.Query; | |
public class GenericDAO<T> implements Serializable { | |
private static final long serialVersionUID = 1L; | |
@Inject | |
private EntityManager manager; | |
public void remover(T objeto) { | |
Object o = manager.merge(objeto); | |
manager.remove(o); | |
} | |
public void remover(Class<T> classe, Long id) { | |
Object object = manager.find(classe, id); | |
manager.remove(object); | |
} | |
public T buscarPorId(Class<T> classe, Long id) { | |
return manager.find(classe, id); | |
} | |
// Outros métodos | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package base.controle; | |
import java.io.Serializable; | |
import java.util.List; | |
import javax.annotation.PostConstruct; | |
import javax.faces.view.ViewScoped; | |
import javax.inject.Inject; | |
import javax.inject.Named; | |
import base.modelo.Processo; | |
import base.service.ProcessoService; | |
import dao.GenericDAO; | |
import util.ExibirMensagem; | |
import util.FecharDialog; | |
import util.Mensagem; | |
@ViewScoped | |
@Named("processoMB") | |
public class ProcessoMB implements Serializable { | |
private static final long serialVersionUID = 1L; | |
private Processo processo; | |
private ProcessoService processoService; | |
private List<Processo> listaProcesso; | |
private GenericDAO<Processo> daoProcesso; | |
public ProcessoMB() { | |
} | |
@Inject | |
public ProcessoMB(ProcessoService processoService, GenericDAO<Processo> daoProcesso) { | |
this.processoService = processoService; | |
this.daoProcesso = daoProcesso; | |
} | |
@PostConstruct | |
public void inicializar() { | |
operacoesDeAlteracao(); | |
} | |
public void excluir(Processo processo) { | |
try { | |
daoProcesso.remover(Processo.class, processo.getId()); | |
ExibirMensagem.exibirMensagem(Mensagem.SUCESSO); | |
operacoesDeAlteracao(); | |
} catch (Exception e) { | |
ExibirMensagem.exibirMensagem(Mensagem.ERRO); | |
e.printStackTrace(); | |
} | |
} | |
private void operacoesDeAlteracao() { | |
criarNovoProcesso(); | |
atualizarListaProcesso(); | |
} | |
public void criarNovoProcesso() { | |
processo = new Processo(); | |
} | |
public void atualizarListaProcesso() { | |
setListaProcesso(daoProcesso.listarCodicaoLivre(Processo.class, "id_processo != 0")); | |
} | |
public void preencherProcesso(Processo processo) { | |
this.processo = processo; | |
} | |
public Processo getProcesso() { | |
return processo; | |
} | |
public void setProcesso(Processo processo) { | |
this.processo = processo; | |
} | |
public List<Processo> getListaProcesso() { | |
return listaProcesso; | |
} | |
public void setListaProcesso(List<Processo> listaProcesso) { | |
this.listaProcesso = listaProcesso; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment