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:
[code]
- public class Usuario {
-
@Id
-
@GeneratedValue
-
@Column(name = "id_usuario")
-
private int id;
-
@OneToOne
-
@JoinColumn(name = "id_pessoa")
-
@Cascade(CascadeType.SAVE_UPDATE)
-
@ForeignKey(name = "fk_pessoa")
-
private Pessoa pessoa;
-
@NotEmpty
-
private String usuario;
-
@NotEmpty
-
private String senha;
-
@NotEmpty
-
private String confrimarSenha;
-
//get e set [/code]
Endereco:
[code]
- @Entity
- public class Endereco {
-
@Id
-
@GeneratedValue
-
@Column(name = "id_endereco")
-
private int id;
-
private String logradouro;
-
private String numero;
-
private String complemeto;
-
private String cep;
-
private String bairro;
-
private String cidade;
-
private String estado;
-
@OneToMany
-
@JoinColumn(name = "id_pessoa")
-
@Cascade(CascadeType.SAVE_UPDATE)
-
@ForeignKey(name = "fk_pessoa")
-
private Pessoa pessoa;
-
// get e set... [/code]
Contatos:
[code]
- @Entity
- public class Contatos {
-
@Id
-
@GeneratedValue
-
private int id;
-
@ManyToMany
-
@JoinColumn(name = "id_pessoa")
-
@Cascade(CascadeType.SAVE_UPDATE)
-
@ForeignKey(name = "fk_pessoa")
-
private Pessoa pessoa;
-
@NotEmpty
-
private String nome;
-
private String telefone;
-
@Email
-
private String email;
-
private int idade;
-
private String endereco; [/code]
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!?