Edit Record Using Jpa

0

Good morning

Personal,

I'm trying to edit registry.

Well the scenario is the following I have my edit screen that is bringing me the data Correctly.

However when I do the editing it updates on the screen more in the non bank.

I have the following tables as below.

table = person as the following id property, name

table = employee as the following id, job, and people_id (fk) property

I have My classes Repositories As below for each entity ie Person and Officer

package br.com.repository;

import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import br.com.model.FuncionarioModel;


public class FuncionarioRepository implements Serializable {
    private static final long serialVersionUID = 1L;
    private EntityManager manager;

    @Inject
    public FuncionarioRepository(EntityManager manager) {
        this.manager = manager;
    }

    public FuncionarioModel porId(Long id) {
        return manager.find(FuncionarioModel.class, id);
    }

    public List<FuncionarioModel> todos() {
        TypedQuery<FuncionarioModel> query = manager.createQuery("from FuncionarioModel", FuncionarioModel.class);
        return query.getResultList();
    }

    public FuncionarioModel guardar(FuncionarioModel funcionario) {
        return this.manager.merge(funcionario);
    }

    public void remover(FuncionarioModel funcionario) {
        this.manager.remove(funcionario);
    }

    public List<String> cargosQueContem(String cargo) {
        TypedQuery<String> query = manager.createQuery(
                "select distinct cargo from Funcionario " + "where upper(cargo) like upper(:cargo)",
                String.class);
        query.setParameter("cargo", "%" + cargo + "%");
        return query.getResultList();
    }

}
package br.com.repository;

import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import br.com.model.PessoaModel;


public class PessoaRepository implements Serializable {
    private static final long serialVersionUID = 1L;
    private EntityManager manager;

    @Inject
    public PessoaRepository(EntityManager manager) {
        this.manager = manager;
    }

    public PessoaModel porId(Long id) {
        return manager.find(PessoaModel.class, id);
    }

    public List<PessoaModel> todas() {
        TypedQuery<PessoaModel> query = manager.createQuery("from PessoaModel", PessoaModel.class);
        return query.getResultList();
    }

    public PessoaModel guardar(PessoaModel pessoa) {
        return this.manager.merge(pessoa);
    }

    public void remover(PessoaModel pessoa) {
        this.manager.remove(pessoa);
    }

    public List<String> nomesQueContem(String nome) {
        TypedQuery<String> query = manager.createQuery(
                "select distinct nome from Pessoa " + "where upper(nome) like upper(:nome)",
                String.class);
        query.setParameter("nome", "%" + nome + "%");
        return query.getResultList();
    }

}

and my controller classes as below

package br.com.controller;

import java.io.Serializable;
import javax.inject.Inject;

import br.com.model.PessoaModel;
import br.com.repository.PessoaRepository;
import br.com.util.NegocioException;
import br.com.util.Transactional;

public class PessoaController implements Serializable {
    private static final long serialVersionUID = 1L;

    @Inject
    private PessoaRepository pessoaRepository;

    @Transactional
    public PessoaModel salvar(PessoaModel pessoaModel) throws NegocioException {
        if (pessoaModel.getNome().isEmpty()) {
            throw new NegocioException("Nome da Pessoa Não pode ser vazio ");
        }
        return this.pessoaRepository.guardar(pessoaModel);
    }

    @Transactional
    public void excluir(PessoaModel pessoaModel) throws NegocioException {
        pessoaModel = this.pessoaRepository.porId(pessoaModel.getCodigo());
        if (pessoaModel.getNome() == null) {
            throw new NegocioException("Não é possível excluir um Funcionario Demitido!");
        }
        this.pessoaRepository.remover(pessoaModel);
    }

    @Transactional
    public void atualizar(PessoaModel pessoaModel) throws NegocioException {
        pessoaModel = this.pessoaRepository.porId(pessoaModel.getCodigo());
    }

}
package br.com.controller;

import java.io.Serializable;

import javax.inject.Inject;

import br.com.model.FuncionarioModel;
import br.com.repository.FuncionarioRepository;
import br.com.util.NegocioException;
import br.com.util.Transactional;

public class FuncionarioController implements Serializable {
    private static final long serialVersionUID = 1L;

    @Inject
    private FuncionarioRepository funcionarioRepository;

    @Transactional
    public void salvar(FuncionarioModel funcionarioModel) throws NegocioException {
        if (funcionarioModel.getCargo().isEmpty() ) {
            throw new NegocioException("Não é Possivel Salvar Funcionario sem Cargo");
        }
        this.funcionarioRepository.guardar(funcionarioModel);
    }

    @Transactional
    public void excluir(FuncionarioModel funcionarioModel) throws NegocioException {
        funcionarioModel = this.funcionarioRepository.porId(funcionarioModel.getCodigo());
        if (funcionarioModel.getCargo()== null) {
            throw new NegocioException("Não é possível excluir um Funcionario Demitido!");
        }
        this.funcionarioRepository.remover(funcionarioModel);
    }

    @Transactional
    public void atualizar(FuncionarioModel funcionarioModel) throws NegocioException {
        funcionarioModel = this.funcionarioRepository.porId(funcionarioModel.getCodigo());
    }


}

and lastly my Bean class

package br.com.view;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.inject.Produces;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.controller.FuncionarioController;
import br.com.controller.PessoaController;
import br.com.model.FuncionarioModel;
import br.com.model.PessoaModel;
import br.com.repository.FuncionarioRepository;
import br.com.util.NegocioException;

@Named
@ViewScoped
public class ConsultaFuncionariosBean implements Serializable {
    private static final long serialVersionUID = 1L;

    @Inject
    private FuncionarioController funcionarioController;

    @Inject
    private PessoaController pessoaController;

    @Inject transient
    private FuncionarioRepository funcionarioRepository;

    @Produces
    private List<FuncionarioModel> funcionarios;

    @Inject transient
    private FuncionarioModel funcionarioModel;

    @Inject transient
    private PessoaModel pessoaModel;


    @PostConstruct
    public void consultar() {
        this.funcionarios = funcionarioRepository.todos();
    }

    public void Excluir(FuncionarioModel funcionarioModel) throws NegocioException{
        this.funcionarioController.excluir(funcionarioModel);
        this.pessoaController.excluir(funcionarioModel.getPessoaModel());
        //Atualiza o Registro na Tela Assim que For Excluído
        this.consultar();
    }

    //Carrega as Informações de Um funcionario para ser Editada.
    public void Editar(FuncionarioModel funcionarioModel) throws NegocioException{
        this.funcionarioModel = funcionarioModel;
    }

    //Atualiza Registro Alterado
    public void AlterarRegistro() throws NegocioException{
        this.funcionarioController.atualizar(funcionarioModel);
        this.pessoaController.atualizar(funcionarioModel.getPessoaModel());
    }

    public List<FuncionarioModel> getFuncionarios() {
        return funcionarios;
    }

    public void setFuncionarios(List<FuncionarioModel> funcionarios) {
        this.funcionarios = funcionarios;
    }

    public FuncionarioModel getFuncionarioModel() {
        return funcionarioModel;
    }

    public void setFuncionarioModel(FuncionarioModel funcionarioModel) {
        this.funcionarioModel = funcionarioModel;
    }

}

If someone has already experienced this or know how I can resolve it, I am grateful.

I found that after finding the object by the find of the marge it was returned in my get the result of the bank not of the form, I solved the following way I created a variable I saved the result of the form as below, but I found that it is not the Correct solution if someone can correct me.

    @Transactional
public void atualizar(FuncionarioModel funcionarioModel) throws NegocioException {
    String cargo = funcionarioModel.getCargo(); 
    funcionarioModel = this.funcionarioRepository.porId(funcionarioModel.getCodigo());
    funcionarioModel.setCargo(cargo);

}
    
asked by anonymous 23.02.2017 / 14:53

1 answer

0
The merge command is to make the entity manageable by your EntityManager , but if you want to save entity changes to the database, you should call the flush () , or even open and close a transaction, which also does the service though the first method is faster.
public PessoaModel guardar(PessoaModel pessoa) {
    return this.manager.merge(pessoa);
}

This section above only makes the entity in "manageable" mode by EntityManager, call the manager.flush (); method to save the data in the database.

public PessoaModel guardar(PessoaModel pessoa) {
    this.manager.merge(pessoa);
    this.manager.flush();
    return pessoa;
}
    
24.02.2017 / 03:43