Estou trabalhando em uma aplicação que usa JSF 2 e EJB 3. Quando dou primeiro update e depois um create, só o create funciona e update não, se eu tiro o create o update
funciona. Troquei o update para resfresh, colquei flush e nada. Alguém sabe oque ue ta pegando ?
Obrigado.
@Stateless
public class HistoricoAtendimentoFacede extends
AbstractFacade<HistoricoAtendimento> {
public HistoricoAtendimentoFacede() {
super(HistoricoAtendimento.class);
}
@PersistenceContext(unitName = "siab")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public void salvarHistorico(Senha senha) {
Map<String, Object> params = new HashMap<String, Object>();
Date dataInic = AlpsDateUtils.createCalendarWithTrueDateStart(
new Date(), true);
Date dataFim = AlpsDateUtils.createCalendarWithTrueDateStart(
new Date(), false);
params.put("datainic", dataInic);
params.put("datafim", dataFim);
params.put("numero", senha.getNumero());
Object idUltimoHistorico = findParamObject(
" select MAX(h.id) from HistoricoAtendimento h"
+ " where h.dataHoraOrigem between :datainic and :datafim "
+ " and h.senha.numero = :numero ", params);
if (idUltimoHistorico != null) {
*** AQUI EU DOU UM UPDATE **** atualizaHistorico(senha, idUltimoHistorico);
*** E AQUI UM CREATE ****** if (senha.getStatus() != Senha.STATUS.CANCELAR.getValor()
&& senha.getStatus() != Senha.STATUS.FINALIZADA.getValor()) {
HistoricoAtendimento histAted = new HistoricoAtendimento();
histAted.setSetorAtendimentoBySetorOrigemId(senha
.getSetorAtendimento());
histAted.setDataHoraOrigem(new Date());
create(histAted);
}
} else {
HistoricoAtendimento histAtend = new HistoricoAtendimento();
histAtend.setSenha(senha);
histAtend.setSetorAtendimentoBySetorOrigemId(senha
.getSetorAtendimento());
histAtend.setDataHoraOrigem(new Date());
create(histAtend);
}
}
private void atualizaHistorico(Senha senha, Object idUltimoHistorico) {
HistoricoAtendimento hAtend = find(Long.valueOf(String
.valueOf(idUltimoHistorico)));
hAtend.setSetorAtendimentoBySetorDestinoId(senha
.getSetorAtendimento());
hAtend.setDataHoraDestino(new Date());
refresh(hAtend);
}
}
public void refresh(T entity){
getEntityManager().refresh(entity);
getEntityManager().flush();
}
public void create(T entity) {
getEntityManager().clear();
getEntityManager().persist(entity);
}
public T update(T entity) {
entity = getEntityManager().merge(entity);
getEntityManager().flush();
return entity;
}