Erro com as anotações

4 respostas
M

Alguém já deparou com o seguinte erro:

org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: svl.pojos.Midia.itemsReserva
	at org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder(CollectionBinder.java:241)
	at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1371)
	at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:733)

4 Respostas

leofernandesmo

Eu nunca me deparei …mas parece ser simples.
Vc esta tentando mapear usando as anotações @OneToMany ou @ManyToMany
para um objeto que nao implementa uma collection.
Cola o seu código para identificarmos…

M
@Entity
@Table(name="item")
public class Item {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
	
	@Column(length=50)
	private String nome;
}
import java.io.Serializable;
import java.util.Date;

import javax.persistence.*;

@Entity
@Table(name="itemreserva")
public class ItensReserva {
	@EmbeddedId
	Ids id;
	@ManyToOne(fetch=FetchType.EAGER)
	private Item item;
	
	@Embeddable
	public class Ids implements Serializable {
		private Long idItem;
		private Long idReserva;
}
@Entity
@Table(name="reserva")
public class Reserva {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
	
	@EmbeddedId
	private ItensReserva itensReserva;
	
	
	@Column
	private Date data;
}
Mas, agora alterei um pouco.
H

Maneira Correta

@ManyToOne(fetch=FetchType.EAGER) private Collection<Item> item;

Na classe Item coloque um atributo

@ManyToOne(fetch=FetchType.EAGER) private Collection<ItensReserva> itensReserva;

Deve funcionar

smkk

só complementando, ao invés de Collection você pode preferir utilizar List, Set, etc(para isso não é necessário alguma outra configuração, desde que a classe implemente Collection).

e não é necessário adicionar o many-to-one na classe Item, só o adicione se for realmente utilizar.

Criado 20 de outubro de 2007
Ultima resposta 22 de out. de 2007
Respostas 4
Participantes 4