Problems recovering data via JPA changed directly in related entity

0

I have the following entity

@Entity
@Table(name="matricula")
public class Matricula implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "usuario_id", nullable = false)
    private Usuario usuario;

    //...
}   

The Enrollment Entity belongs to a student (user) and only one student. But I have a problem. When I look for the enrollment after changing the given direction in the student, although in the database the information is already updated. in the routine still comes the old data.

User Entity.

@Entity
@Table(name="usuario")
public class Usuario implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Enumerated(EnumType.STRING)
    @Column(name="tipo_do_cartao")
    private TipoDeCartao cartao;
    //---
}

Persisting user change.

// Methodos.
public String confirma(){
    if(novoTipoDeCartao == null || novoTipoDeCartao.equals(usuario.getCartao()) ){
        FacesUtil.addErrorMessage("O novo tipo de cartão não pode ser nulo ou não pode ser igual ao atual");
        return "";
    }
    try{
        usuario.setCartao(novoTipoDeCartao);
        servicoUsuario.trocarTipoDeCartao(usuario);
        return "/Index.xhtml";
    } catch (NegocioException e){
        FacesUtil.addErrorMessage(e.getMessage());
        return "";
    }
}

@Transacao
    public void trocarTipoDeCartao(Usuario usuario) {
        Matricula matriculaAtiva = servicoMatricula.matriculaAtiva(usuario.getId());
        servicoDocumento.cancelaDocumentos(matriculaAtiva,EstadoDoDocumento.VENCIDO_TROCA);
        usuario = usuarioDao.salvar(usuario);
        matriculaAtiva = servicoMatricula.matriculaAtiva(usuario.getId());
    }

public Usuario salvar(Usuario usuario) {
        System.out.println("-- UsuarioDao salvar-----");
        try {
            usuario = entityManager.merge(usuario);
            //entityManager.flush();
            return usuario;
        }catch (PersistenceException e) {
            e.printStackTrace();
            //throw new NegocioException("Email já cadastrado");
            throw new NegocioException(TrataErro.buscaErro(e,"Não foi possivel salvar"));
        }catch (Exception e) {
            e.printStackTrace();
            //throw new NegocioException("Email já cadastrado");
            throw new NegocioException(TrataErro.buscaErro(e,"Não foi possivel salvar (E)"));
        }   
    }

Search for enrollment

public Matricula matriculaAtiva(Long usuarioId) {
        return matriculaDao.buscarMatriculaAtiva(usuarioId);
    }

public Matricula buscarMatriculaAtiva(Long idUsuarioLogado) {
        try {
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<Matricula> cq = cb.createQuery(Matricula.class);
            Root<Matricula> m = cq.from(Matricula.class);
            cq.select(m);
            List<Predicate> criteria = new ArrayList<>();

            criteria.add(cb.equal(m.<Long>get("usuario").get("id"),idUsuarioLogado));
            criteria.add(cb.equal(m.<Boolean>get("status"),true));

            cq.where(criteria.toArray(new Predicate[0]));
            cq.orderBy(cb.asc(m.get("id")));
            TypedQuery<Matricula> tq = entityManager.createQuery(cq);

            Matricula mat = tq.getSingleResult();
            return mat;

        } catch (NoResultException e) {
            return null;
        }
    }
    
asked by anonymous 02.12.2016 / 16:37

0 answers