-1
Good morning
Personal,
I’m trying to edit record.
well the scenario is the following have my screen edit that is bringing me the data correctly.
but when I do the editing it updates on the screen no more in the bank.
I have the following tables as below.
table = person as the following property id,name
table = employee as the following property id,position and id_person(fk)
I have My Repositories classes As below for each Employee or Employee
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 by Ultimo my class Bean
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;
}
}
In case anyone’s been through it or knows how to fix it I’m grateful
personal found that after I find the object by find of Marge it was returned in my get the result of the database not form, I solved as follows I created a variable saved the result of the form as below, but I thought it’s not the right 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);
}
Select the code and click Ctrl + K or click on the formatting button, makes it nicely formatted =)
– Jéf Bueno
I can’t remember if it’s the
merge
or theupdate
, but there’s one of them that you have to do.flush()
(something like).– igventurelli
Thanks Igor for answering, you wouldn’t have an example of how I can do this ?
– JavaX_javaX
in fact I would like to use the get and set methods of my Entity or to take the data that were changed in the form as example = personal.setEntity.setName(personal.getName()), so it would be a more correct way to treat the object, more when I do so it returns the database data and not the form changed.
– JavaX_javaX
Just do
seuEntityManager.flush()
shortly after the.merge(T)
. I see you’re using themerge
in methods calledguarda
. If you want to add a record you should use.persist(T)
. Themerge
andupdate
only serve to update information already existing in the bank.– igventurelli