Org.hibernate.AnnotationException

1 resposta
johmjohm

Ola galera!!

quero fazer o seguinte:
uma Pessoa só pode ter UM Usuario e UM Endereco mas pode ter Varios contatos!!

Esta aki minhas classes:

Pessoa:
1. @Entity  
   2. public class Pessoa {  
   3.       
   4.     @Id  
   5.     @GeneratedValue  
   6.     @Column(name = "id_pessoa")  
   7.     private int id;  
   8.       
   9.     @NotEmpty  
  10.     @Column(name="nome_pessoa")  
  11.     private String nome;  
  12.     @Column(name = "sobrenome_pessoa")  
  13.     private String sobenome;  
  14.     @NotEmpty  
  15.     @Column(name = "cpf_pessoa")  
  16.     private String cpf;  
  17.     @Column(name = "idade_pessoa")  
  18.     private int idade;  
  19.     @Column(name = "telefone_pessoa")  
  20.     private int telefone;  
  21.     @Email  
  22.     @Column(name = "email_pessoa")  
  23.     private String email;  
  24.       
  25.     @OneToOne  
  26.     @JoinColumn(name = "id_usuario")  
  27.     @Cascade(CascadeType.SAVE_UPDATE)  
  28.     @ForeignKey(name = "fk_usuario")  
  29.     private Usuario usario;  
  30.       
  31.     @OneToOne  
  32.     @JoinColumn(name = "id_endereco")  
  33.     @Cascade(CascadeType.SAVE_UPDATE)  
  34.     @ForeignKey(name = "fk_endereco")  
  35.     private Endereco endereco;  
  36.     //getters e setters
Usuario:
1. public class Usuario {  
   2.       
   3.     @Id  
   4.     @GeneratedValue  
   5.     @Column(name = "id_usuario")  
   6.     private int id;  
   7.       
   8.     @OneToOne  
   9.     @JoinColumn(name = "id_pessoa")  
  10.     @Cascade(CascadeType.SAVE_UPDATE)  
  11.     @ForeignKey(name = "fk_pessoa")  
  12.     private Pessoa pessoa;  
  13.       
  14.     @NotEmpty  
  15.     private String usuario;  
  16.     @NotEmpty  
  17.     private String senha;  
  18.     @NotEmpty  
  19.     private String confrimarSenha;  
  20.     //get e set
Endereco:
1. @Entity  
   2. public class Endereco {  
   3.       
   4.     @Id  
   5.     @GeneratedValue  
   6.     @Column(name = "id_endereco")  
   7.     private int id;  
   8.       
   9.     private String logradouro;  
  10.     private String numero;  
  11.     private String complemeto;  
  12.     private String cep;  
  13.     private String bairro;  
  14.     private String cidade;  
  15.     private String estado;  
  16.       
  17.     @OneToMany  
  18.     @JoinColumn(name = "id_pessoa")  
  19.     @Cascade(CascadeType.SAVE_UPDATE)  
  20.     @ForeignKey(name = "fk_pessoa")  
  21.     private Pessoa pessoa;  
  22.     // get e set...
Contatos:
1. @Entity  
   2. public class Contatos {  
   3.       
   4.     @Id  
   5.     @GeneratedValue  
   6.     private int id;  
   7.       
   8.     @ManyToMany  
   9.     @JoinColumn(name = "id_pessoa")  
  10.     @Cascade(CascadeType.SAVE_UPDATE)  
  11.     @ForeignKey(name = "fk_pessoa")  
  12.     private Pessoa pessoa;  
  13.     @NotEmpty  
  14.     private String nome;  
  15.     private String telefone;  
  16.     @Email  
  17.     private String email;  
  18.     private int idade;  
  19.     private String endereco;

E a classe para gerar a tabela:

1. public class GeraTabela {  
   2.   
   3.     /** 
   4.      * @param args 
   5.      */  
   6.     public static void main(String[] args) {  
   7.         // TODO Auto-generated method stub  
   8.   
   9.         AnnotationConfiguration cfg = new AnnotationConfiguration();  
  10.         cfg.configure();  
  11.           
  12.         SchemaExport se = new SchemaExport(cfg);  
  13.         se.create(true, true);  
  14.           
  15.     }  
  16.   
  17. }

e da o seguinte error:
[color=red]
Exception in thread "main" org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: br.com.vetta.entidades.Endereco.pessoa
at org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder(CollectionBinder.java:266)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1448)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:803)
at org.hibernate.tool.hbm2ddl.SchemaExport.(SchemaExport.java:128)
at org.hibernate.tool.hbm2ddl.SchemaExport.(SchemaExport.java:91)
at br.com.vetta.teste.GeraTabela.main(GeraTabela.java:17)[/color]

Alguem poderia me informar pq ta dando este erro?
Estou meio confuso se fiz as assossiacoes corretamente!?

1 Resposta

J

Na classe endereço tu informa que o mapeamento é um para um entre pessoa e endereco, e no endereco tu informa um um para muitos para pessoa, isso ja está errado, se for usar o mesmo relacionamento de forma bidirecional.

Mas no caso, o erro está no trecho @OneToMany @JoinColumn(name = "id_pessoa") @Cascade(CascadeType.SAVE_UPDATE) @ForeignKey(name = "fk_pessoa") private Pessoa pessoa;
pois o hibernate espera como atributo uma list, collection ou set de Pessoa, não um objeto Pessoa, pois esta sendo informado que é um para muitos, no caso, um endereco contem varias pessoas.
O mesmo erro se repete em @ManyToMany @JoinColumn(name = "id_pessoa") @Cascade(CascadeType.SAVE_UPDATE) @ForeignKey(name = "fk_pessoa") private Pessoa pessoa;

uma vez que está sendo esperado a list, collection ou set e está encontrando apenas um objeto.

Criado 17 de março de 2011
Ultima resposta 17 de mar. de 2011
Respostas 1
Participantes 2