Boa noite, galera.
Estou tentando criar um relacionamento 1:1 só que estou gerando uma exception…
Tenho duas classes :
Pessoa.java
package wer.com.br.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import com.sun.istack.internal.NotNull;
@Entity
@Table(name = "tbl_pessoa")
public class Pessoa {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer idPessoa;
@Column(name = "Nome")
@NotNull
private String nome;
@Column(name = "Email")
private String email;
@Column(name = "Anotacoes")
private String anotacoes;
@OneToOne(mappedBy ="idPessoa")
@Cascade(CascadeType.ALL)
private Telefone telefones;
public Pessoa(Integer idPessoa, String nome, String email,
String anotacoes, Telefone telefones) {
super();
this.idPessoa = idPessoa;
this.nome = nome;
this.email = email;
this.anotacoes = anotacoes;
this.telefones = telefones;
}
@Override
public String toString() {
return "Pessoa [anotacoes=" + anotacoes + ", email=" + email
+ ", idPessoa=" + idPessoa + ", nome=" + nome + ", telefones="
+ telefones + "]";
}
public Pessoa() {
this.telefones = new Telefone();
}
public Integer getIdPessoa() {
return idPessoa;
}
public void setIdPessoa(Integer idPessoa) {
this.idPessoa = idPessoa;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAnotacoes() {
return anotacoes;
}
public void setAnotacoes(String anotacoes) {
this.anotacoes = anotacoes;
}
public Telefone getTelefones() {
return telefones;
}
public void setTelefones(Telefone telefones) {
this.telefones = telefones;
}
}
Telefone.java
package wer.com.br.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "tbl_telefone")
public class Telefone {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private Integer idTelefone;
@Column(name="Comercial")
private String comercial;
@Column(name="Residencial")
private String residencial;
@Column(name="Ramal")
private String ramal;
@OneToOne
@JoinColumn(name="id_pessoa_FK")
private Pessoa pessoa;
@Override
public String toString() {
return "Telefone [comercial=" + comercial + ", idTelefone="
+ idTelefone + ", ramal=" + ramal + ", residencial="
+ residencial + "]";
}
public Telefone() {
}
public Telefone(Integer idTelefone, String comercial, String residencial,
String ramal) {
super();
this.idTelefone = idTelefone;
this.comercial = comercial;
this.residencial = residencial;
this.ramal = ramal;
}
public Integer getIdTelefone() {
return idTelefone;
}
public void setIdTelefone(Integer idTelefone) {
this.idTelefone = idTelefone;
}
public String getComercial() {
return comercial;
}
public void setComercial(String comercial) {
this.comercial = comercial;
}
public String getResidencial() {
return residencial;
}
public void setResidencial(String residencial) {
this.residencial = residencial;
}
public String getRamal() {
return ramal;
}
public void setRamal(String ramal) {
this.ramal = ramal;
}
public Pessoa getPessoa() {
return pessoa;
}
public void setPessoa(Pessoa pessoa) {
this.pessoa = pessoa;
}
}
Erro:
INFO: Bind entity wer.com.br.entity.Telefone on table tbl_telefone
Exception in thread “main” org.hibernate.AnnotationException: Unknown mappedBy in: wer.com.br.entity.Pessoa.telefones, referenced property unknown: wer.com.br.entity.Telefone.idPessoa
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:136)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:324)
at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:756)
at org.hibernate.tool.hbm2ddl.SchemaExport.(SchemaExport.java:93)
at org.hibernate.tool.hbm2ddl.SchemaExport.(SchemaExport.java:61)
at wer.com.br.main.Main.main(Main.java:11)
O que deve está fazendo errado ?