Boa Tarde!
Estou com um problema minha HQL esta retornando valores repetidos eu tenho duas entidades Pessoa e PessoaTelefone, se na PessoaTelefone tem 2 registros o HQL retorna 2 registros repetidos e assim por diante
Models
public class Pessoa implements java.io.Serializable {
private int codigo;
private String nome;
private List<Pessoatelefone> pessoatelefones = new ArrayList<Pessoatelefone>();
....
....
@OneToMany(mappedBy = "pessoa", fetch = FetchType.EAGER, targetEntity = Pessoatelefone.class,
cascade=CascadeType.ALL, orphanRemoval = true)
//@Fetch(FetchMode.JOIN)
public List<Pessoatelefone> getPessoatelefones() {
return pessoatelefones;
}
}
public class Pessoatelefone implements java.io.Serializable {
private int codigopessoatelefone;
private Pessoa pessoa;
private String numerotelefone;
....
....
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "CODIGOPESSOA", nullable=false)
public Pessoa getPessoa() {
return this.pessoa;
}
}
Trecho que consulta
public List<Pessoa> consultarPessoaHQL(Pessoa p_Pessoa) throws Exception {
EntityManagerFactory emf = null;
EntityManager em = null;
try {
emf = Persistence.createEntityManagerFactory("teste");
em = emf.createEntityManager();
Query q = em.createQuery("FROM Pessoa p JOIN FETCH p.pessoatelefones WHERE p.codigo = ?1");
q.setParameter(1, p_Pessoa.getCodigo());
return (List<Pessoa>) q.getResultList();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (em != null) {
em.close();
}
if (emf != null) {
emf.close();
}
EntityManagerFactoryImpl empImpl = (EntityManagerFactoryImpl) emf;
System.out.println(empImpl.getSessionFactory().getStatistics());
}
}
Fiz um for com a Lista de Pessoa
Codigo: 4
Nome: Pessoa4
Sexo: M
Data Nascimento: null
Telefones:
Codigo Telefone: 7
Número Telefone: 33333333
Codigo Telefone: 8
Número Telefone: 44444444
------------------------------------------------
Codigo: 4
Nome: Pessoa4
Sexo: M
Data Nascimento: null
Telefones:
Codigo Telefone: 7
Número Telefone: 33333333
Codigo Telefone: 8
Número Telefone: 44444444
------------------------------------------------