Doubt (hibernate) - JSF

Asked

Viewed 52 times

0

I’m making an employee register, and this register already has departments registered in the bank. So, at this point I walk through the database to present the data in the view, but when I finish the registration, this "department" selected earlier, instead of just registering it as the department id, it is creating a new department and saves in the employee table the new id.
Method to recover departments:

    public List getProjetos() {

        ProjetoDAO projetoDAO = new ProjetoDAO();
        List<Projeto> listaProjeto = projetoDAO.listarProjeto();

        return listaProjeto;
    }

Another problem I’m having is that I’m doing Lazy the moment I call the "department" get, I know the problem is here, because whenever he gives the get, will create a new.

    public Projeto getProjeto() {

        if (projeto == null) {

        projeto = new Projeto();

        }

        return projeto;

        }
    }

Working class

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "tbl_funcionario_has_tbl_projeto", joinColumns = {
        @JoinColumn(name = "tbl_funcionario_fun_codigo") }, inverseJoinColumns = {
                @JoinColumn(name = "tbl_projeto_pro_codigo") })
private List<Projeto> projeto;

The doubt is how to make him verify that already exists that department and not create a new?

  • Have you noted the relationship in the entities? Post the entities. The table in the bank is a single (employee department) or separate?

  • She’s separated... I posted the relationship there...

  • Your doubt is based on creating a new method or you suspect that your notes are wrong?

  • My question is how to do when calling the get of the Project, it does not create another project but recover the id and save in the employee table.

1 answer

0

This is hard to understand. I suggest separating into two posts, one for each problem. And post all related entities.

Anyway let me try the staff.

It seems to me a @Onetoone relationship of Employee to Department.
something like that:

@Entity
public class Funcionario{
   @Id
   private Integer funcionario;
   private String nome;
   @OneToOne
   private Departamento departamento
   // get and sets
}

@Entity
public class Departamento{
   @Id
   private Integer departamento;
   private String nome;
   // get and sets

}

Recalling that the department object persisted should be assigned to employee.

  • Marcelo, problem is when registering a new employee, when I select the project I want the employee to be registered, instead of just saving the project with his id, he is creating a new project, with the same information.

  • It is that this can be a problem in the mapping of the entity or in the DAO. See how this the superscript of the equals method of its entity. What may be happening? By persisting ( merge or persist ) JPA does not recognize the already persisted object. See if the object in question is being managed by Entity manager. (http://www.architecturacomputational.eti.br/2013/02/entenda-o-ciclo-de-life). If nothing resolves. Only seeing the full code.

  • Got it, I’ll take a look at this documentation you gave me, thank you.

Browser other questions tagged

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