-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Problem with relationship
PostPosted: Thu Feb 04, 2010 12:36 pm 
Newbie

Joined: Thu Feb 04, 2010 10:10 am
Posts: 8
Hi all,

I have this relationship:

Departamento.java
Code:
@Entity
@Table(name="departamento")
public class Departamento
{
   private Long id;
   private String nome;
   private Date dataCriacao;
   private Date dataAlteracao;
   
   private List<Recurso> cargos;


Cargo.java
Code:
@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.java
Code:
@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


Top
 Profile  
 
 Post subject: Re: Problem with relationship
PostPosted: Thu Feb 04, 2010 1:23 pm 
Beginner
Beginner

Joined: Wed Nov 12, 2008 12:07 pm
Posts: 21
You have to use .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) in your criteria
But if you want a descent explanation about it - that I'm not able to give - just google DISTINCT_ROOT_ENTITY.
Blz?

Roberto


Top
 Profile  
 
 Post subject: Re: Problem with relationship
PostPosted: Thu Feb 04, 2010 2:09 pm 
Newbie

Joined: Thu Feb 04, 2010 10:10 am
Posts: 8
Hi,

It work perfectly! Thanks, Roberto! I will google this and learn more about!

Valeu, cara! ;)

abs.
André Vendramini


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.