Doubt (Hibernate) - JSF

0

I am making an employee registration, and in that register already have departments registered in the bank. So, at that moment I run the bank to present the data in the view, but when I finish the registration, this previously selected "department", instead of just registering it as the department id, it is creating a new department and saved 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 the lazy at the moment it calls the get of the "department", I know the problem is here, because whenever it gives the get, it will create a new one.

    public Projeto getProjeto() {

        if (projeto == null) {

        projeto = new Projeto();

        }

        return projeto;

        }
    }

Employee 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;

Doubt is how to make it check that this department already exists and not create a new one?

    
asked by anonymous 18.05.2016 / 13:45

1 answer

0

This is difficult to understand. I suggest separate in two post, one for each problem. And post all related entities.

Anyway let me try the employee's.

Looks like a relationship @OneToOne from Employee to Department. something like this:

@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

}

Remembering that Persistent department object should be assigned to an employee.

    
19.05.2016 / 21:08