Salve galera!
Gostaria de saber de vcs qual a melhor opção de mapeamento em JPA
Collection, List ou Set
Realizando testes com JPA+Hibernate 3.5.0 Final descobri o seguinte:
Considerando as seguintes Entitys Class:
Pessoa (Pessoa possui uma lista de Email e Endereços)
Email
Endereco
Usando Set<>
1° Se eu uso Set<> com fetch=FetchType.EAGER
nos mapeamentos funciona! Os seja, o metodo find do EntityManager traz os Sets preenchidos!
2° Se eu uso Set<> com fetch=FetchType.LAZY
nos mapeamentos o método find do EntityManager retorna o seguinte erro:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:
- Quando utilizo Set mesmo sem fazer
fetch=FetchType.EAGER
consigo fazer o seguinte jp ql:
select p from Pessoa p
join fetch p.emailSet em
join fetch p.enderecoSet en
where p.pesId = 1
4. Outro problema que encontrei utilizando Set<> é o seguinte:
Set não aceita dados duplicados na lista como por exemplo uma lista de nomes:
Paulo
Paulo
Felipe
Se adiciono estes nomes em um Set os dados duplicados serão descartados ficando assim:
Paulo
Felipe
=====================================================================================
Usando List<>
1° Se eu uso List<> com fetch=FetchType.EAGER
nos mapeamentos o método find do EntityManager retorna o seguinte erro:
org.hibernate.HibernateException: cannot simultaneously fetch multiple bags
2° Se eu uso List<> com fetch=FetchType.LAZY
nos mapeamentos o método find do EntityManager retorna o seguinte erro:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:
- Quando utilizo List de nenhuma maneira consigo fazer o seguinte jp ql:
select p from Pessoa p
join fetch p.emailList em
join fetch p.enderecoList en
where p.pesId = 1
pois tenho o seguinte erro:
org.hibernate.HibernateException: cannot simultaneously fetch multiple bags
- Utilizando List eu consigo fazer o seguinte jp ql e funciona!
select p from Pessoa p join fetch p.emailList
Só não consigo entender porque funciona com Set e não Funciona com List alguem puder me explicar? Devo utilizar Collection, List ou Set? Onde estou me perdendo?