Correct way to update an object property

Asked

Viewed 104 times

0

I created an update method, however I am having to create a variable to update my position property, which although it is working, I think this would not be the correct way to treat an object.

I have this property in the object Funciomodel follows below the code.

public void atualizar(FuncionarioModel funcionarioModel) throws NegocioException {
    String cargo = funcionarioModel.getCargo();
    funcionarioModel = this.funcionarioRepository.porId(funcionarioModel.getCodigo());
    funcionarioModel.setCargo(cargo);
    if (cargo.isEmpty()) {
        throw new NegocioException("Não é possível fazer a Alteração campo cargo está vazio !");

    }
}

if anyone knows how I can fix this.

  • can you be a little more explicit about what you want? I’m not sure if you want to rewrite that method or not....

  • Thanks Vitor for the return, I wish I didn’t have to create exactly this line in the code String cargo = functionModel.getCargo(); as I am object oriented I thought I would just use = String cargo =functionModel.setCargo(post); and it would be enough there it does not update. that is, if I don’t create String post = functionModel.getCargo(); to store what comes in the form, it takes that line functionModel.setCargo(post); what is in the database is that it doesn’t update.

1 answer

1


If you want to eliminate the line "String cargo = funcionarioModel.getCargo();" you can do so:

public void atualizar(FuncionarioModel funcionarioModel) throws NegocioException {  
    if (funcionarioModel.getCargo().isEmpty()) {
        throw new NegocioException("Não é possível fazer a Alteração campo cargo está vazio !");
    }  
    this.funcionarioRepository.porId(funcionarioModel.getCodigo()).setCargo(funcionarioModel.getCargo());
}

Note that you should check whether you will launch an Exception before making the change, because if you launch it after making your change "this.funcionarioRepository" will have an invalid (empty) Position even after Exception has been launched.

  • Thank you very much Douglas, I was already hopeless to answer someone I appreciate I will test the code here.

  • I tested the code here, on the fly that’s right, now that’s the correct way to treat an Object. Thanks again Douglas

  • If this answer solved the problem, please mark it as "accept", ok?

  • as I mark ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.