Boa tarde!
Estou usando hibernate /jpa2, tenho 2 entidades Pessoa e Pessoatelefone, uma pessoa tem varios telefones.
Estou colocando o @FetchMode.JOIN mais mesmo assim o hibernate faz 2 consultas ao inves de uma, o que pode ser?
Valeu.
mapeamento classe Pessoa
@OneToMany(mappedBy = "pessoa", fetch = FetchType.EAGER, targetEntity = Pessoatelefone.class,
cascade=CascadeType.ALL, orphanRemoval = true)
@Fetch(value=FetchMode.JOIN)
public List<Pessoatelefone> getPessoatelefones() {
return pessoatelefones;
}
mapeamento classe Pessoatelefone
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "CODIGOPESSOA", nullable=false)
public Pessoa getPessoa() {
return this.pessoa;
}
hql
EntityManagerFactory emf = null;
EntityManager em = null;
try {
emf = Persistence.createEntityManagerFactory("teste");
em = emf.createEntityManager();
Query q = em.createQuery("FROM Pessoa p WHERE p.codigo = ?1");
q.setParameter(1, p_Pessoa.getCodigo());
return (List<Pessoa>)q.getResultList();
//EntityManagerFactoryImpl empImpl = (EntityManagerFactoryImpl)emf;
//System.out.println(empImpl.getSessionFactory().getStatistics());
} 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());
}