Hi all,
I have this relationship:
Departamento.javaCode:
@Entity
@Table(name="departamento")
public class Departamento
{
private Long id;
private String nome;
private Date dataCriacao;
private Date dataAlteracao;
private List<Recurso> cargos;
Cargo.javaCode:
@Entity
@Table(name="cargo")
public class Cargo implements Recurso
{
private Long id;
private String nome;
private double salarioBase;
private Date dataCriacao;
private Date dataAlteracao;
private Empresa empresa;
private Departamento departamento;
private List<Funcionario> funcionarios;
@OneToMany(targetEntity=Funcionario.class, mappedBy="cargo")
@JoinColumn(name="func_id")
public List<Funcionario> getFuncionarios()
{
return funcionarios;
}
public void setFuncionarios(List<Funcionario> funcionarios)
{
this.funcionarios = funcionarios;
}
Funcionario.javaCode:
@Entity
@Table(name="funcionario")
public class Funcionario implements Recurso
{
private Long id;
private String nome;
private double salario;
private Date dataCriacao;
private Date dataAlteracao;
private Cargo cargo;
@ManyToOne(targetEntity=Cargo.class)
@JoinColumn(name="fk_cargoId")
@ForeignKey(name="cargo_id")
public Cargo getCargo()
{
return cargo;
}
public void setCargo(Cargo cargo)
{
this.cargo = cargo;
}
When i do this query
WITHOUT the
fetch=FetchType.EAGER on the annotations at Departamento.java:
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(Departamento.class).addOrder(Order.asc("nome"));
return (List<Departamento>) criteria.list();
It returns me the correctly data:
Code:
ATENDIMENTO
CRIAÇAO
PLANEJAMENTO
PROJETOS
TECNOLOGIA
I have 5 Departamento. It's OK.
Each Departamento has its own Cargo, and each Cargo has MANY Funcionario. Well, the problem is: when i put
fetch=FetchType.EAGER on the annotations at Departamento.java to tell Hibernate to bring all the relationship data, it returns one "Departamento" for each "Cargo" it has.
Code:
ATENDIMENTO
CRIAÇAO
CRIAÇAO
CRIAÇAO
PLANEJAMENTO
PROJETOS
TECNOLOGIA
TECNOLOGIA
It has duplicated CRIAÇAO 3 times, cause CRIAÇAO has 3 Cargo. But why?
Regards,
André Vendramini