Pessoal bom dia, estou com uma duvida aqui nos mapeamentos do Hibernate, sou novato então desculpe se tiver falando bobeira aqui…
Dei uma pesquisada, sobre o assunto e não consigo acertar, tenho em uma classe de testes dois Lists de String ai salvo no banco até ai tudo certo porem na hora de recuperar tenho problemas, as listas não vem populada devido ao LAZY, porem quando mudo para EAGER da o erro a baixo.
Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
at org.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:94)
at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:98)
at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:66)
at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:56)
at org.hibernate.loader.entity.BatchingEntityLoader.createBatchingEntityLoader(BatchingEntityLoader.java:126)
at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1765)
at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1769)
at org.hibernate.persister.entity.AbstractEntityPersister.createLoaders(AbstractEntityPersister.java:3002)
at org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:2995)
at org.hibernate.persister.entity.SingleTableEntityPersister.postInstantiate(SingleTableEntityPersister.java:712)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:329)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:11)
at util.HibernateUtil.<clinit>(HibernateUtil.java:8)
... 1 more
Tentei manter o LAZY tipo no meu DAO fazes os gets e como esta na transação iria pupular “teoricamente”.
Segue a baixo meu DAO
public AnimalVO buscaPorId(Integer id) throws Exception {
Session sessao = null;
AnimalVO animalVO = new AnimalVO();
try {
sessao = HibernateUtil.getSessionFactory().openSession();
animalVO = (AnimalVO) sessao.get(AnimalVO.class, id);
animalVO.getDoencas();
animalVO.getAlergias();
} catch (Exception e) {
animalVO = null;
throw new Exception(Mensagens.erroPesquisaPorCodigo);
} finally {
sessao.close();
}
return animalVO;
}
Aqui esta como esta agora o mapeamento
@Entity
@Table(name = "animais")
public class AnimalVO {
@Id
@GeneratedValue
private Integer id;
@CollectionOfElements(fetch = FetchType.LAZY)
private List<String> alergias;
@CollectionOfElements(fetch = FetchType.LAZY)
private List<String> doencas;
//gets sets
}
Se alguem puder ajudar, agradeço, estranho é que grava no banco tudo certo porem não retorna.