Hibernate + @CollectionOfElements [RESOLVIDO]

1 resposta
L

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.

1 Resposta

L

Pessoal Resolvido para quem tiver o mesmo problema…!! na verdade eu estava errando no List o hibernate não aceita multiplos List… desculpe devia ter pesquisado melhor… a resposta estava em Estrategia carregar para multiplas coleções com o hibernate - > http://www.guj.com.br/java/192576-estrategia-carregar-para-multiplas-colecoes-com-o-hibernate

Então como o sugerido mudei o List para Set e funcionou perfeitamente… diferenças entre Set e List -> http://codeflavor.wordpress.com/2010/04/07/list-set-map-o-que-e-e-como-usar/

@Entity
@Table(name = "animais")
public class AnimalVO {

	@Id
	@GeneratedValue
	private Integer id;
	@CollectionOfElements(fetch = FetchType.EAGER)
	private Set<String> alergias;

	@CollectionOfElements(fetch = FetchType.EAGER)
	private Set<String> doencas;
//gets sets
}
Criado 6 de junho de 2011
Ultima resposta 11 de jun. de 2011
Respostas 1
Participantes 1